Thread: Reversing words in a string

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Reversing words in a string

    Hey I'm trying to reverse the words in a string, but I can seem to get it right. I am able to reverse the first word, but not the second.

    Here is my strategy -

    Reverse the entire string .
    Reverse each word in the new string.

    This is what I have so far -

    Code:
    void RevWords(char** str)
    {
    	char temp[255];
    	int i = 0,j = 0,index;
    	int len;
    	StrRev(str);
    
    	while(*(*str + i) != '\0')
    	{
    		int k =0;
    		len = GetSpaces((*str + j)) - 1;//returns the length until a space is reached
    		while(*(*str + j) != ' '){		
    			index = ((*str + i) + len - k) - *str;
    			temp[j] = *(*str + index);
    			++j,++k;
    		}
    		i += len;
    	}
    	temp[i] = '\0';
    	*str = temp;
    }
    The output is -
    Code:
    The original string is hello world.
    The revesed string is world╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╬i╠Ω.
    Press any key to continue . . .
    Thanks!
    also, could someone look at my other question as well - Inplace stack reversal(recursive)
    Last edited by rocketman03; 09-06-2009 at 11:13 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    *str = temp;
    OOPS! temp is a local array and as such ceases to exist after the function ends.

    As to seeing what (else) happened here, you should check whether StrRev gives you what you want, and if so trace your loop in a debugger to watch what happens.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    OOPS! temp is a local array and as such ceases to exist after the function ends.
    I get the same result even if I print out the string inside the function. StrRev just reverses the string, I tested it with and without the function and it seems to be working. Well, I fixed the extra characters in the end, it should have been
    Code:
    temp[j] = '\0';
    but my output is still the same -
    Code:
    The original string is hello world.
    The revesed string is world.
    Press any key to continue . . .

    for some reason my second word is being chopped off..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So trace your loop in a debugger to see what happens. I suspect your space thing is off by one, but I'm not sure.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    aah..you were right, a few off by one errors. Heres the updated code, seems to be working now.
    Code:
    void RevWords(char** str)
    {
            char temp[255];
    	int i = 0,j = 0,index;
    	int len;
    	StrRev(str);
    
    	while(*(*str + i) != '\0')
    	{
    		int k =0;
    		len = GetSpaces((*str + j)) - 1;
    		while(*(*str + j) != ' ' && *(*str + j) != '\0'){		
    			index = ((*str + i) + len - k) - *str;
    			temp[j] = *(*str + index);
    			++j,++k;
    		}
    		temp[j++] = ' ';
    		i += len + 1;
    	}
    	temp[j] = '\0';
    	*str = temp;
    }
    The original string is hello world.
    The revesed string is world hello .
    Press any key to continue . . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 8
    Last Post: 03-31-2006, 08:15 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM