Like Tree1Likes
  • 1 Post By std10093

Help Reverse Sentence C Program

This is a discussion on Help Reverse Sentence C Program within the C Programming forums, part of the General Programming Boards category; Im writing a c program that reverses the words in a sentence, Example: you can cage a swallow can't you? ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Help Reverse Sentence C Program

    Im writing a c program that reverses the words in a sentence,
    Example:
    you can cage a swallow can't you?
    you can't swallow a cage can you?

    I have it all working, except the fact that I dont know how to get the words themselves to turn around. Heres my code and an example of the output im getting.

    Output Im getting:
    Enter a sentence: you can cage a swallow can't you?
    Reverse of sentence: uoy t'nac wollaws a egac nac uoy?

    My Code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define MAX 200 /*Decent number of chars for a sentence*/
         
    int main()
    {
        /*Initializations*/
      char forward_sentence[MAX];
      char ch, termch;
      int i = 0, j, len = 0;
      
      /*Gets a sentence from the user*/         
      printf("Enter a sentence: ");
      ch = getchar();
    
      /*Loop goes as long as it doesnt hit a terminating char*/
      while(i < MAX && ch != '\n' && ch != '.' && ch != '?' && ch != '!')
      {
         forward_sentence[i] = ch;
         i++;
         len++;
         ch = getchar();
      }
    
      /*Sets the terminating characters*/
      for(i = 0; i < MAX; i++)
      {
       if(ch == '.')
        termch = '.';
       else if(ch == '?')
        termch = '?';
       else if(ch == '!')
        termch = '!';
       }
    
      /*Prints out the sentence reversed*/     
      printf("Reverse of sentence: ");
      for(j = len - 1; j >= 0; j--)
      {
       if(forward_sentence[j] == ' ')
        {
                     
      }
      printf("%c", forward_sentence[j]);
      }
         
       printf("%c\n", termch)
       return 0;
     }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Code:
     while(i < MAX
    Do you think that's enough? Is there space for the null terminator if the user really inputs as many characters as the value of MAX? I would have MAX-1 ..
    Well, actually, I know see that you never put a null terminator at your sentence. That's dangerous.
    Output Im getting:
    Enter a sentence: you can cage a swallow can't you?
    Reverse of sentence: uoy t'nac wollaws a egac nac uoy?
    From here, you see what you doing. You go at the end of the sentence and you start printing the letters of every word in reverse order. Your logic should be to go at the end of the sentence, find where the last word begins, print that word in normal order. Then do the same with the prelast word and so on. A linked list would be an easy solution (you would print every node of the list from the last to the head, but I don't recommend it for this application).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Ok, I made an example. You should be inspired by it and do the logic (by modifying the code) to fit your code.
    See my comments.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char sentence[50] = "My name, is George Samaras";
        
        // to be at the end of the word
        char* end = &sentence[strlen(sentence)];
        
        // like i counter in for loop
        char* worker;
        
        // 1 if the word that we print now is
        // not the first word of the sentence
        // This is a flag in other words
        int notFirstWord = 1;
        
        // a space, dot, etc.
        char separator;
        
        // to be at the start of the word
        char* start = &sentence[strlen(sentence)-1];
        
        // while we have something to read
        while(*end != sentence[0])
        {
            // while start point to a letter
            // (could use isalpha from ctype.h)
            while(*start != ' ' && *start != '.' && *start != ',')
            {
                // decrease start pointer until it reaches something
                // that's not a letter
                start--;
                
                // if it is the 1st word
                // change the variable notFirstWord
                if(*start == sentence[0])
                {
                    notFirstWord = 0;
                    break;
                }
            }
            
            // start points to the separator
            separator = *start;
            
            // if it is not the first word, then we need to move start one position
            // to point to the 1st letter of the word. Print until we find the end 
            // of the word
            for(worker=start+notFirstWord ; worker != end ; worker++)
                printf("%c", *worker);
            if(notFirstWord)
                printf("%c", separator);
         
            // update end pointer
            end = start;
            
            // go the previous character
            start--;
        }
        
        return 0;
    }
    Now, I got to go to a party, so enjoy coding without me around!! Ciao!!
    Crossfire likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse the words of a sentence.
    By Mr.Lnx in forum C Programming
    Replies: 9
    Last Post: 04-24-2012, 02:15 AM
  2. Program to count parts of a sentence
    By dustynlily in forum C Programming
    Replies: 11
    Last Post: 02-20-2012, 08:01 PM
  3. Here is my Sentence Analyzer Program!
    By jeremy duncan in forum C Programming
    Replies: 7
    Last Post: 01-02-2012, 11:04 AM
  4. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  5. Help with Sentence fix string program
    By daywalker- in forum C++ Programming
    Replies: 9
    Last Post: 11-01-2007, 06:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21