Like Tree1Likes
  • 1 Post By Salem

Reverse the words of a sentence.

This is a discussion on Reverse the words of a sentence. within the C Programming forums, part of the General Programming Boards category; Hello to everyone. I am trying to solve this exercise : Enter a sentence: you can jump can't you? Reversal ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    283

    Reverse the words of a sentence.

    Hello to everyone.

    I am trying to solve this exercise :

    Enter a sentence: you can jump can't you?
    Reversal of sentence: you can't jump can you?

    So far , I have this code...

    Code:
    #include <stdio.h>
    int main(void)
    {
      char ch , array[100]={'\0'};
      int i=0 ,  count ;
      
      printf("Enter a sentence: ");
      
      while((ch=getchar())!='.' && ch != '?' && ch != '!' )
    	  array[i++]=ch;
      
      for(count=0; count<i; count++)  
      printf("%c" , array[count]);
      
      putchar(ch);  // print single character 
      
      printf("\n Reversal of the words of the sentence: ");
      
      for(count= i-1; count>=0; count--)
      {
    	  if(ch != ' ')
    	  {
    	 
      }
    	  
      }
    
      putchar(ch);
    	  
        return 0;
    }
    I must to solve it without pointers or strings. :/

    I only want an idea..... some similar code without pointers for this.
    Only with arrays.

    Thank you in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well in your counting backward loop, all the interesting stuff seems to happen when you reach a space.

    So perhaps do something when you hit a space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    283
    Quote Originally Posted by Salem View Post
    Well in your counting backward loop, all the interesting stuff seems to happen when you reach a space.

    So perhaps do something when you hit a space.
    The space does nothing for now.

    Code:
     you can jump can't you?
    'PSEUCODE' ->
    Code:
      IF you are in the beginning of the last word the character is -> ' '
    You fill the array and you are in the 'u' point (cause I store '?' in a separate char variable) hence.... can't(HERE)you? is the beginning of the last word that I want to appear it first... and then.... the next-last word (pre-last) jump(HERE)can't .

    (HERE) <=> ' ' (for this example only)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Here's a fun thing to try
    if ( array[count] == ' ' ) printf("%s\n", &array[count] );
    ZuK likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    283
    Code:
    #include <stdio.h>
    int main(void)
    {
      char ch , array[100]={'\0'};
      int i=0 ,  count;
      
      printf("Enter a sentence: ");
      
      while((ch=getchar())!='.' && ch != '?' && ch != '!' )
    	  array[i++]=ch;
      
      for(count=0; count<i; count++)  
      printf("%c" , array[count]);
      
      putchar(ch);  // print single character 
      
      printf("\n Reversal of the words of the sentence: ");
    
      count=i-1;
      while( count >=0 )
      {
    	  int j=0;
    	  
    	  if( ch == ' ')
    	  putchar(ch);
    	  
    	  else
    			{
    				 array[j] = array[count];
    				 ++j;
    			 }
    			 
    		  count--;
      }
    
      putchar(ch);
    	  
        return 0;
    }
    I want to solve the given exercise.

    Why I don't take the proper result?

    I give -> Hello man.
    and take -> . (empty) why? :S

    The problem is that the book does not have a solution... if it had I would have read it from there.
    Last edited by Mr.Lnx; 04-19-2012 at 12:55 PM.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to toss this code out, grab a piece of paper, and think. Write out how to do it in English. There are a few possibilities. Here's one.

    Use strtok to set spaces after words to '\0', saving the returned pointers in an array. Then go backwards through the pointer array, printing the words. You'll have to deal with end punctuation specially to keep it at the end (if that's what you want), and also change the case of the original first word to lower and the original last word to upper (if you want).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    283
    hello again.

    Code:
    #include <stdio.h>
    int main(void)
    {
      char ch , array[100]={'\0'};
      int i=0 , count , j;
      
      printf("Enter a sentence: ");
      
      while((ch=getchar())!='.' && ch != '?' && ch != '!' )
      {
    	  array[i++]=ch;
    	  putchar(ch);
      }
       
      putchar(ch);  // print single character 
      
      printf("\n Reversal of the characters of the words of the sentence: ");
      
      for(count= i-1; count>=0; count--)
      {
    
      if( array[count] == ' ' )
      {
    	  j=count+1;
    	  
    	  while(j>=0 || j<=count)
    	  { 
          printf("%c" , array[j]);
          ++j;
      }
    }
    }
      
      putchar(ch);
    	  
        return 0;
    }
    I take an error

    Code:
     ./tests
    Enter a sentence: hello man.
    hello man.
     Reversal of the characters of the words of the sentence: man�P�<m��ƍ��@S�#C#2��@�ƍ�S�#e�)>S��y�6J>� @�ƍ�@�ƍ�9@�ƍ��%ƍ��%ƍ�&ƍ�-&ƍ�P&ƍ�[&ƍ�k&ƍ��&ƍ��&ƍ�'ƍ�'ƍ�,ƍ�(,ƍ�Z,ƍ��,ƍ��,ƍ�-ƍ�I-ƍ�_-ƍ��-ƍ��-ƍ��-ƍ��-ƍ�.ƍ�9.ƍ�M.ƍ�^.ƍ�f.ƍ�x.ƍ��.ƍ��.ƍ��.ƍ�R/ƍ�r/ƍ�/ƍ��/ƍ��/ƍ��/ƍ�!�ߍ�����d@@8	���   @
    The output that I want is :

    Code:
     Enter a sentence: Hello man.
    Hello man.
    
    Reversal of the characters of the words of the sentence: man Hello.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    i think here you need two nested for loops one increments after it hits the ' ' and the other continues decrementing until it finds another ' ' or you create a two dimensional array that increments after the ' ' is entered and then increments the array.for example str[MAX][]; for ( ch = getc(stdin) == ' ') i++;str[i][m++] = ch; i think this may work.....

  9. #9
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    283
    Quote Originally Posted by Nyah Check View Post
    i think here you need two nested for loops one increments after it hits the ' ' and the other continues decrementing until it finds another ' ' or you create a two dimensional array that increments after the ' ' is entered and then increments the array.for example str[MAX][]; for ( ch = getc(stdin) == ' ') i++;str[i][m++] = ch; i think this may work.....

    Well , thank you very much for your answer! I try to fix it with this ->

    Code:
    /* Αντιστροφή της σειράς των λεξεων μιας πρότασης
     *  INPUT : Hello man. 
     *  OUTPUT : man Hello.
     * */
     //---------------------------------------------------------------------
     
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      char ch , array[100]={'\0'};
      int i=0 , count , j;
      
      printf("Enter a sentence: ");
      
      while((ch=getchar())!='.' && ch != '?' && ch != '!' )
      {
    	  array[i++]=ch;
    	  putchar(ch);
      }
       
      putchar(ch);  // print single character 
      
      printf("\n Reversal of the characters of the words of the sentence: ");
      
      for(count= i-1; count>= 0; count--)
      {
      
      if( array[count] == ' ' || count == 0 )
      {
            if(count == 0)
    	   j=0;
    	
    	else
    	j=count+1;  
    	
    	while(isalpha(array[j]))
    	{
          printf("%c" , array[j]);
          ++j;
        
        }
         
        printf(" ");
     }
    
    }
      
      putchar(ch);
    	  
        return 0;
    }
    //----------------------------------------------------------------------
    // An dwsw apostrofo tin trwei opws episis kai to keno stin arxi :/
    Ignore my comments , They are in Greeks. (I can explain what I write if you want to of course!!!)

    Anyway I have[s] two[/s] one problem... it doesn't print the apostrophe.

    Do you have any idea in order to fix that without change the logic of my program??? (I mean not completely changes).

    Thank you in advance for your time.
    Last edited by Mr.Lnx; 04-23-2012 at 08:02 PM.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    I thoroughly recommend the two-pass approach for this. It operates in place, and is a bijective transform!

    Pass 1: Just reverse the whole string.
    Pass 2: Locate the end of each backwards word and reverse that word back again.

    Note that you can do the passes in either order too.
    Oh and in your case I guess you would exclude the last character from any manipulation, if it is a punctuation character.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Split sentence into words
    By password in forum C Programming
    Replies: 4
    Last Post: 07-05-2010, 07:12 AM
  2. how to split a sentence to words
    By sivapc in forum C++ Programming
    Replies: 13
    Last Post: 09-28-2009, 01:21 AM
  3. Dividing sentence into words
    By Lord CyKill in forum C Programming
    Replies: 1
    Last Post: 11-02-2003, 06:25 AM
  4. How to count the words in sentence ?
    By Th3-SeA in forum C Programming
    Replies: 1
    Last Post: 10-01-2003, 01:34 AM
  5. Searching for words within a sentence
    By drdroid in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:09 PM

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