Reversing string

This is a discussion on Reversing string within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a string say, "My name is John" I want to reverse it like, "yM eman si nhoJ" ...

  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,175
    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,806
    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
    682
    While we are at C, strtok() maybe?
    I never put signature, but I decided to make an exception.

  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 12:54 PM. Reason: almost forgot to free!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    4,415
    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, 04:18 PM
  2. reversing a string
    By quasigreat in forum C Programming
    Replies: 11
    Last Post: 05-22-2008, 07:24 AM
  3. Reversing string
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2006, 07:50 PM
  4. Reversing a String in C
    By Ice in forum C Programming
    Replies: 6
    Last Post: 04-18-2002, 11:55 AM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 01:20 PM

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