So far, the answers have made alternative algorithms that can be sorted into two classes:
- Cancel all sentence and all words in it. Some versions change the words first, while others reject the sentence first; the order in which these turns are applied does not matter. The net effect is that the words appear in the reverse order, but the characters inside each word are in their normal order.
- Put the words on the stack, then grab them from the stack again so that the last word comes first.
Instead of suggesting another alternative algorithm (which is likely to fall into one of the two above categories), I will explain why the code in the original question does not work properly.
First, note that the code in the question is actually a variant of class 1. First, it cancels the entire sentence and then changes each word:
for(i = 99; i >= 0; i--) { if(sentence[i] == ' ') { while(sentence[i + j] != ' ') { printf("%c", sentence[i + j]); j++; } j = 1; printf(" "); } }
Besides some beginner mistakes, this is actually the best way to change sentence words. It does not use additional memory and does not waste time.
The respondent noted that the algorithm works as intended, except that it does not print the first word of the original sentence (which should be the last word). The reason for this is that array traversal stops at ' ' in both directions. When the outer loop reaches the beginning of the sentence, it does not find a space, because the first character of user input overwrites the space in sentence[0] :
char sentence[100] = { ' ' }, c, tc; int i = 0, j = 1, size = 0; printf("Enter a sentence: \n"); for(c = getchar(); (c != '.') && (c != '!') && (c != '?') && (c != '\n'); c = getchar(), i++) { sentence[i] = c; size++; }
Therefore, when i becomes 0 in the outer loop, there is no place, and the inner loop that should print a word starting with sentence[0] is never entered. i then decreases to -1 , and the outer loop ends.
You can test this without changing the code by simply running the program as a user. If you enter a space as the first character, the program response will be correct:
Enter a sentence: you can cage a swallow can't you? you can't swallow a cage can you?
There are two ways that you can ensure that the first word is included in your code. First, just always put a space at the beginning of your sentence array. You can do this by starting to copy user input to i = 1 instead of i = 0 :
int i = 1, j = 1, size = 0;
Another, slightly less elegant way would be to simply repeat the inner loop after the end of the outer loop:
for(i = 99; i >= 0; i--) { if(sentence[i] == ' ') { while(sentence[i + j] != ' ') { printf("%c", sentence[i + j]); j++; } j = 1; printf(" "); } } while(sentence[i + j] != ' ') { printf("%c", sentence[i + j]); j++; }
You can make this less repetitive by dividing the inner loop into a new function. Here, the entire algorithm with the inner loop is factorized by the print_word function, skipping comments and blank lines:
#include<stdio.h> void print_word(char[] sentence, int i) { int j = 1; while(sentence[i + j] != ' ') { printf("%c", sentence[i + j]); j++; } } int main(void) { char sentence[100] = { ' ' }, c, tc; int i = 0, j = 1, size = 0; printf("Enter a sentence: \n"); for(c = getchar(); (c != '.') && (c != '!') && (c != '?') && (c != '\n'); c = getchar(), i++) { sentence[i] = c; /* Store the current character in the array */ size++; /* Increase the sentence size */ } tc = c; /* Get the terminating character */ for(i = 99; i >= 0; i--) { if(sentence[i] == ' ') { print_word(sentence, i); printf(" "); /* Print a tailing space */ } } print_word(sentence, i); printf("\b%c\n", tc); return 0; /* Return 0 upon successful program execution */ }
As a final note, there is one more thing you could do better. Right now, the outer loop begins with i = 99 , the last possible character in the sentence array. However, when reading user input, you updated i to indicate the next input position, so before starting the outer loop, i already points to the first character after the sentence. Why not use this and just start with i - 1 ?