Thread: Reversing string

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Reversing string

    Hello,

    I have a string say,

    "My name is John"

    I want to reverse it like,

    "yM eman si nhoJ"

    I wrote a program for it which compiles but doesn't give answer. Can anyone tell me what is the problem in my program. Thanks a lot for all your help

    Code:
    void reverse(char *str){
         char * temp = str;
         char substr[10];
         int i = 0, j = 0, k = 0;
         while(*temp != '\0'){
                     if(*temp != ' '){
                              substr[i] = *temp;
                              i++;
                              }
                     else{
                          if(i>0){
                                   for(j=i; j>0;i--){
                                           *str++ = substr[j];
                                           }
                                           *str++ = ' ';
                                           k = 1;
                      }
                      }
                      if(k = 1){
                          i = 0;
                           j = 0;
                           k = 0;
                          }
                 
                 }
                 *str = '\0';
         }
    
    int main(){
        char * text = "My name is John";
        cout<<text<<endl;    
        reverse(text);
        cout<<text;
        system("PAUSE");
        }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    for(j=i; j>0;i--)  // Which variable are you decrementing here?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, under most circumstances I would expect that code to crash.
    Code:
    char * text = "My name is John";
    This is a pointer to a string literal which the address of is passed into your reverse function. The reverse function then accesses and attempts to modify this string literal which is a big no-no. Doing this usually, but not always, causes a program to crash. As a start, you should replace that line of code with the following:
    Code:
    char text[] = "My name is John";
    This makes text an array and not a pointer to a string literal. As an array, you can pass its address to the reverse function and that code can make modifications to the array without a crash... assuming you aren't doing anything else that's unsafe.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    While we are at C, strtok() maybe?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void Reverse(char *str)
      { char *x = strdup(str);
        char *y = str + (strlen(str) -1);
        while (*x)
           *y-- = *x++;
         free(x);  }
    Last edited by CommonTater; 05-11-2011 at 11:54 AM. Reason: almost forgot to free!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jsgn
    I have a string say,

    "My name is John"

    I want to reverse it like,

    "yM eman si nhoJ"
    The way I see it, you have two problems to solve:
    • How to break up the input into tokens separated by a space?
    • How to reverse a token?


    A C++ style solution to the first problem would involve reading token by token into a std::string object using the overloaded operator>> for input streams. A C++ style solution to the second problem would involve say, the std::reverse generic algorithm, or something along those lines (possibly written by yourself, since this is presumably for practice).

    Note that you can quite easily reverse a string in-place; extra memory proportional to the string length is unnecessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    The way I see it, you have two problems to solve:
    • How to break up the input into tokens separated by a space?
    • How to reverse a token?


    A C++ style solution to the first problem would involve reading token by token into a std::string object using the overloaded operator>> for input streams. A C++ style solution to the second problem would involve say, the std::reverse generic algorithm, or something along those lines (possibly written by yourself, since this is presumably for practice).

    Note that you can quite easily reverse a string in-place; extra memory proportional to the string length is unnecessary.
    Hi Lase... I forgot where I was there for a second... just love playing with pointers. If course your advice is better for C++ than mine.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    2
    Thanks for your replies. I made some modifications to my code but still I am not able to get right answer. Can anyone see what is wrong in my logic and help me out. Thanks

    Code:
    void reverse(char *str){
         char * temp = str;
         char substr[10];
         int i = 0, j = 0, k = 0;
         while(*temp != '\0'){
                     if(*temp != ' '){
                              substr[i] = *temp;
                              i++;
                              temp++;
                              }
                     else{
                          if(i>0){
                                   for(j=i; j>0;j--){
                                           *str++ = substr[j];
                                           }
                                           *str++ = ' ';
                                           k = 1;
                      }
                      }
                      if(k == 1){
                          i = 0;
                           j = 0;
                           k = 0;
                          }
                 
                 }
                 *str = '\0';
         }
    
    int main(){
        char text[] = "My name is John";
        cout<<text<<endl;    
        reverse(text);
        cout<<text;
        system("PAUSE");
        }

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You need to walk through the algorithm for doing what you want bit by bit.

    As far as I can see, you have nothing right.

    You need to reverse each substring within a string. Revise your code to print each substring in the order it was passed first, and then try and figure out what to do next.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reversing a string
    By swappo in forum C++ Programming
    Replies: 6
    Last Post: 06-14-2009, 03:18 PM
  2. reversing a string
    By quasigreat in forum C Programming
    Replies: 11
    Last Post: 05-22-2008, 06:24 AM
  3. Reversing string
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2006, 06:50 PM
  4. Reversing a String in C
    By Ice in forum C Programming
    Replies: 6
    Last Post: 04-18-2002, 10:55 AM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM