# rostring ## Conceptual Overview ### The Problem We are asked to write a program that is given a string as argument. It prints the 2nd through last words followed by the first word, with exactly one space between the words printed. [ [Full Subject](subject.en.txt) | [Examples](examples.txt) ] ### Approach Create a huge array of pointers to char. Step through the string passed as argument, converting spaces & tabs to '\0', and as the beginning of each word is found, store a pointer to it in the array. Print the 2nd through last words in the array followed by a space, then print the first word. This approach is sloppy and tedious to code, but easy to debug. ## Pseudocode ``` If exactly 0 arguments have been given print a newline and exit set char *str to the beginning of the first argument create an array named "words" containing several thousand char * pointers initialized to NULL set int i to 0 while str points to a non-'\0' character while str points to a whitespace, non-'\0' character set the character pointed to by str to '\0' increment str forward one character if str points to a non-'\0' character set element i in the array words to the value of str while str points to a non-whitespace, non-'\0' character increment str forward one character if str points to a non-'\0' character set i to its own value plus 1 set int i to 1 while element i of the array words is a non-NULL pointer print the string pointed to by element i of words set i to its own value plus 1 if element 0 of the array words is a non-NULL pointer print the string pointed to by element 0 of words print a newline ``` ## Test cases ``` ./rostring "Que la lumiere soit et la lumiere fut" ./rostring " AkjhZ zLKIJz , 23y" ./rostring "first" "2" "11000000" ./rostring "abc" ./rostring " abc" ./rostring "abc " ./rostring "abc def" ./rostring "abc def ghi " ./rostring "" ./rostring " " ``` ----------------------------------------------- Assignment name : rostring Expected files : rostring.c Allowed functions: write, malloc, free -------------------------------------------------------------------------------- Write a program that takes a string and displays this string after rotating it one word to the left. Thus, the first word becomes the last, and others stay in the same order. A "word" is defined as a part of a string delimited either by spaces/tabs, or by the start/end of the string. Words will be separated by only one space in the output. If there's less than one argument, the program displays \n. Example: $>./rostring "abc " | cat -e abc$ $> $>./rostring "Que la lumiere soit et la lumiere fut" la lumiere soit et la lumiere fut Que $> $>./rostring " AkjhZ zLKIJz , 23y" zLKIJz , 23y AkjhZ $> $>./rostring "first" "2" "11000000" first $> $>./rostring | cat -e $ $> -------------------------------------------------------------------- #include <unistd.h> int skip_whitespace(char *str, int i) { while (str[i] == ' ' || str[i] == '\t') ++i; return (i); } int ft_wordlen(char *str) { int i = 0; while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') ++i; return (i); } int print_word(char *str, int i, int *is_first) { int word_len; i = skip_whitespace(str, i); word_len = ft_wordlen(str + i); if (*is_first == 0) write(1, " ", 1); write(1, str + i, word_len); *is_first = 0; return (i + word_len); } int epur_str(char *str) { int i = 0; int is_first = 1; i = skip_whitespace(str, i); while (str[i] != '\0') { i = print_word(str, i, &is_first); i = skip_whitespace(str, i); } return (is_first); } int main(int argc, char **argv) { if (argc >= 2) { char *str = argv[1]; int i = 0; int is_first; i = skip_whitespace(str, i); i = i + ft_wordlen(str + i); is_first = epur_str(str + i); print_word(str, 0, &is_first); } write(1, "\n", 1); return (0); }