c - Reversing words in a sentence -


I currently use KN I'm going through C programming: a modern approach I have made it before the text for the 8th chapter (erase), and I'm eager to go to Chapter 9, but I have not yet Ultimately, the so-called "programming projects" have to be resolved, unfortunately, the 14th ... bugs me .

Write a program that reverses the words in sentences.

  Enter a sentence: You can make a swallow cage, can not you? Reversal of conviction: You can not swallow a cage?  

sign : to read the characters one by one and to store them in a one-dimensional four array Use a loop for Prevent the loop on any duration, question mark or an exclamation point ("Terminating character"), which is saved in different char variables, then to search backwards through the array for the beginning of the last word Use the second loop Print the last word, then do the backward search for the next-to-last word until the array starts. Finally, print the terminative character.

I am thinking of defining a word as a sequence of characters between the space so when one place is reached, going back to print each place, one goes And the place is found, until there is no other place. My first version of the program printed the first word only. Its current version prints only the second word I have been stuck on this for two days, so any help really is appreciated. Here's my code, as well as an output sample. I hope I have documented my code properly. thank you in advanced!

Code

  / * Include Standard I / O Library * / # & lt; Stdio.h & gt; / * Define main * / Integer (zero) {/ ** * Announce an array of characters accumulating the sentence, Also * A character representing the current character under a cursor and * Terming characters * / Characters Sentences [100] = {''}, C, TC; / ** * Announce a loop counter that was initially initialized to 0, an incremental * variable, as well as the reading sentence * / int i = 0, j = 1, size = 0; / * Get sentence * / printf ("Enter a sentence: \ n"); (C = = '.') For (C = = ..) & amp; (C! = '!') & Amp; (C! = '?') & Amp; (C! = '\ N'); C = getchar (), i ++) {sentence [i] = c; / * Store the current character in the array / size ++; / * Increase in the size of the sentence * /} TC = C; / * Get the Terminator character * / / ** * Go back through the array, print each sequence of characters for the spaces * (/ i = 99; i> = 0; i--) {if (Sentence [i] == '') {While (sentence [i + j]! = '') {Printf ("% c", sentence [i + j]); J ++; } J = 1; / * Incremental variable reset * / printf (""); / * Print a Telling Space * /}} / ** * Remove the tilling spaces and print the terminographic character, as well as a new line * / printf ("\ b% c \ n", TC); Return 0; Return to Successful Program Execution  

Output:

To think about another method:

  You can make a swallow cage, can not you? UAN TNEK What does an EXECEC NAC think of UAC? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you do not have an excel NAC UAL can leave? ^^^^^ Can not you swallow an EXECEC NACUO? ^^^^^^^ Can not you swallow an EXECEC NACUO? ^ Can not you swallow a cage nac uo? ^^^^ Can uoy do not swallow a cage? ^^^ Can not you swallow a cage? ^^^  

For everything you want to reverse (this is a complete sentence or a word):

  1. Find the beginning and end
  2. Swap the start and end characters
  3. Take the "inward" one at a time
  4. Continue until you reach "middle" / Li>

Part of a string is a normal operation behind, it is understandable to create its own function.

  • Beginning Index
  • Closing Index
  • and

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

    The other common thing that needs to be done is "finding" something, it's space or punctuation. You may need to type it, or if you want to use library functions or want a prompt, see:


    Comments