XLR3204S XLR3204S - 1 year ago 60
C Question

Reversing words in a sentence

I'm currently going through K.N. King's C Programming: A Modern Approach. I've made it past the text for the 8th chapter (Arrays), and I'm eager to move on to chapter 9, but I've yet to solve the so-called "programming projects" at the end of each chapter. Unfortunately, the 14th... bugs me.

Write a program that reverses the words in a sentence.

Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't swallow a cage can you?

Hint: Use a loop to read the characters one by one and store them in a one-dimensional char array. Have the loop stop at a period, question mark, or exclamation point (the "terminating character "), which is saved in a separate char variable. Then use a second loop to search backward through the array for the beginning of the last word. Print the last word, then search backward for the next-to-last word. Repeat until the beginning of the array is reached. Finally, print the terminating character.

I've been thinking of defining a word as a sequence of characters between blank spaces. So when a space is reached, go backward, printing each character, until another space is found. My first version of the program only printed the first word. The current version of it only prints the other words. I've been stuck on this for two days, so any help is truly appreciated. Here is my code, as well as an output sample. Hopefully I've properly documented my code. Thanks in advance!


/* Include the standard I/O library */

/* Define main */
int main(void) {

* Declare an array of characters storing the sentence, as well as
* a character representing the current character under cursor and
* the terminating character
char sentence[100] = { ' ' }, c, tc;

* Declare a loop counter already initialized at 0, an incremental
* variable, as well as the size of the read sentence
int i = 0, j = 1, size = 0;

/* Get the sentence */
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's size */

tc = c; /* Get the terminating character */

* Go backward through the array, printing each sequence of characters
* between spaces
for(i = 99; i >= 0; i--) {

if(sentence[i] == ' ') {

while(sentence[i + j] != ' ') {

printf("%c", sentence[i + j]);

j = 1; /* Reset the incremental variable */
printf(" "); /* Print a tailing space */

* Delete the tailing blank space and print the terminating character,
* as well as a new line
printf("\b%c\n", tc);

return 0; /* Return 0 upon successful program execution */



Answer Source

Another methodology to think about:

you can cage a swallow can't you?
uoy t'nac wollaws a egac nac uoy?
you t'nac wollaws a egac nac uoy?
you can't wollaws a egac nac uoy?
you can't swallow a egac nac uoy?
you can't swallow a egac nac uoy?
you can't swallow a cage nac uoy?
you can't swallow a cage can uoy?
you can't swallow a cage can you?

For each thing you want to reverse (be it a whole sentence or a word):

  1. Find the beginning and end
  2. Swap the beginning and end characters
  3. Move "inwards" once
  4. keep going until you reach "the middle"

Since reversing a chunk of a string is a common operation, it makes sense to make it its own function. And since the only information the function need to do its job is:

  1. the string
  2. the beginning index
  3. the ending index

What do you think the parameters for the function would be?

The other common thing that needs to be done over and over is "finding" something, be it a space or a punctuation mark. You may need to write this yourself, or if you can use library functions, or want a hint, look up:

man strcspn
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download