Reversing words

This is a discussion on Reversing words within the C++ Programming forums, part of the General Programming Boards category; I have a small problem with char arrays. I made a function that reverses a char array, which is pretty ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    85

    Reversing words

    I have a small problem with char arrays.
    I made a function that reverses a char array, which is pretty easy but now I want to also reverse the words.

    So if i have char word[] = "hello world"
    a call to PrintStringReverse(word) would give me:
    dlrow olleh

    Code:
    void PrintStringReverse(char s[])
    {
    	int N = strlen(s);
    	for(int i=0;i<N/2;i++)	  
    		swap(s[i],s[N-1-i]);
    	cout << s << endl;
    }
    and a call to PrintWordReverse would give me:
    olleh dlrow

    How can I go about this using the functions built into <cstring> only, no vector or <string> suggestions please.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Make a function 'foo' that works like PrintStringReverse, except that instead of calculating N from strlen(s), pass N as an argument.

    Then, loop through your string, one word at a time (using a pointer whose memory address sits at the front of the word and a variable that you increment forward, probing for spaces, tabs, and newlines), and call 'foo' on each individual word, passing the appropriate value N.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    I'm so confused.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Look for a space or a '\0'(which is at the end of the string) in your char array. If you find a space or a '\0', record the index position. Then reverse the characters between that index position and the previously recorded index position.
    Last edited by 7stud; 12-01-2005 at 01:31 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    void PrintWordsReverse(char s[])
    {	 	 
    	int N = strlen(s);
    	int count=0,lastindex=0,spaces=0;
    	
    	for(int i=0;i<N;i++)			
    	{
    		if(s[i] == ' ')
    		{
    			spaces++;
    			for(lastindex;lastindex<(i/2);lastindex++)
    				swap(s[lastindex],s[i-1-lastindex]);
    			lastindex = i+1;
    
    		}	 
    	}
    	
    	if(spaces == 0)
    	{
    		for(int i=0;i<N/2;i++)
    			swap(s[i],s[N-1-i]);
    	}
    	cout << s << endl;	  
    }
    This is as far as i got, my logic is that I look for a space and a space means a word so just reverse that word then move onto the next word to reverse. The first word works but the second doesn't. Can somebody seem some logic error in my code?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    my logic is that I look for a space
    "hello world"
    Is there a space after "world"?

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You need to account for the fact that there will not be a space after the last world. You could probably work the main loop so that the if(space == 0) stuff isn't needed.

    Also:
    Code:
    for(lastindex;lastindex<(i/2);lastindex++)
    Is probably not what you want. You want to swap characters from lastindex to i. But that conditional isn't right. What if I had a string of length 50, with lastindex equal to 44 at this point in the code? i must be > 44, so 44 / 2 = 22, which isn't greater than 44. The loop never runs, and characters are never swapped.

    Your small inner for() loop makes my head hurt... let's reword it to use an offset variable:
    Code:
    for(offset = 0; offset = (i - lastindex) / 2; offset++)
       swap(s[lastindex + offset], s[i - 1 - offset]);
    Merging that into your function I get this, which works for me:
    Code:
    void PrintWordsReverse(char s[])
    {
    	int N = strlen(s);
    	int count = 0, lastindex = 0, spaces = 0, offset;
    
    	for(int i = 0; i <= N; ++i)
    	{
    		if(s[i] == ' ' || s[i] == 0)
    		{
    			for(offset = 0; offset < (i - lastindex) / 2; ++offset)
    				swap(s[lastindex + offset], s[i - 1 - offset]);
    			lastindex = i + 1;
    		}
    	}
    
    	cout << s << endl;
    }
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    void PrintWordsReverse(char s[])
    {	 	 
    	int N = strlen(s);
    	int count=0,lastindex=0,index=0;
    	
    	for(int i=0;i<N;i++)			
    	{
    		if(s[i] == ' ' || i == (N-1))
    		{
    			if(i==(N-1)) i++;
    			for(index=lastindex;index<(i-((i-lastindex)/2));index++)
    				swap(s[index],s[(i-1)-(index-lastindex)]);
    			lastindex = i+1;
    		}	 
    	}
    	cout << s << endl;	  
    }
    finally me and a friend worked it out. I still get confused by it, though I'll take it as it is.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing Letters in Words of a Sentence
    By CRSpeedy in forum C Programming
    Replies: 10
    Last Post: 03-31-2009, 06:12 PM
  2. Reversing words...noob
    By shardin in forum C Programming
    Replies: 5
    Last Post: 08-23-2007, 11:11 AM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

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