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 */
#include<stdio.h>
/* 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++;
}
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 */
}
http://drp.ly/1nYt5J
Program to reverse words in a given string in C++Reverse the given string str using STL function reverse(). Iterate the reversed string and whenever a space is found reverse the word before that space using the STL function reverse().
push each word on a stack and read the stack from index 0 to N-1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With