Thread: need help with string manipulation.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    need help with string manipulation.

    this code has some bugs Im new to c strings. the code that i have below reverses each word. For example. i entered "house is" it reverses it to "si esuoh" I want it to reverse it by word like "is house".

    My other questions since strings are pointers how do I call each one in an array?

    here is my code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void reverse(char *sPtr);
    
    int main (void)
    
    {
    
    char sentence[80];
    
    printf( "enter a line of text:\n");
    
    fgets(sentence,80, stdin );
    
    printf("\nThe line printed backwards is\n");
    reverse( sentence );
    
    return 0;
    
    }
    
    void reverse ( char *sPtr){
    
    if(sPtr[0] == '\0') {
    	return;}
    
    else {
    	reverse(&sPtr[1]);
    	putchar(sPtr[0]);
    }
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need to break the sentence to each phrase, and then print them reversed.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    reverse it like tokens?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by newbc View Post
    reverse it like tokens?
    Yes, but this time you'll read one word with each iteration.
    Devoted my life to programming...

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I would suggest calling your function every time there's a space, and breaking up strings into individual words.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The solution would be pretty straight forward. Use strtok (although i'm not very about on strtok as it alters the orginal string. but for beginning should be ok) to tokensie the string with the ' ' as a deliminator. And call the the your reverse function on every token.

    Code:
     p = strtok( ... )
     while( p != NULL )
     {
        reverse( p );
        p = strtok( NULL, " " );
     }
    And also why you using recursive method to reverse a function. Would much efficient to just go with the traditional method with looping.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM