/* words1.c * * read and write a list of words * (assigment for Feb 6 from http://cs.marlboro.edu/courses/spring2013/systems) * * version 1 : quick and dirty: * - One big block of memory for word list * - Hard-coded constants. * - No error checking. * - Word entry terminated by word = '-' * * This code works (within the size bounds) but is not what I'd call * good practice. Input with too many characters or words will bleed * into the the next word, or overflow the allocated space and * possibly crash the program. * * $ gcc word1.c -o words1 * $ ./words1 * Enter words separated by whitespace or newline. Type - to stop. * one * two * three * - * The words are [one two three ]. * * $ ./words1 * Enter words separated by whitespace or newline. Type - to stop. * thiswordistoolong * two * three * The words are [thiswordtwo two three ]. * * Jim Mahoney | Feb 2013 | MIT License */ #include int main(){ char buffer[5*8]; // space for 5 words of 8 characters each char* word = buffer; // a pointer to one of the words int n_words = 0; // number of words read. printf("Enter words separated by whitespace or newline. Type - to stop.\n"); while (1){ scanf("%s", word); if (word[0] == '-') break; word += 8; n_words++; } word = buffer; printf("The words are ["); while (n_words--){ printf("%s ", word); word += 8; } printf("].\n"); return 0; }