Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Reverse words of a string C++

  1. Dec 20, 2011 #1
    Write a program to reverse words of a string individually, for example if you enter: I love C++, it should display I evol ++C

    They've given the solution in my textbook but I don't quite understand the logic behind it.

    void main( )
    { int l, i, k = 0 ;
    char str[80], word[80] ;
    cout<<"Enter any string"<<endl ;
    gets(str) ;
    strcat(str, " ") ;
    for(i = 0 ; str !='0' ; i++)
    {
    if(str != ' ')
    { word[k] = str ;
    k = k + 1 ;
    }
    else
    {
    while(k > 0)
    { cout<<word[--k] ;
    }
    cout<<str ;
    }
    }
    getch( ) ;
    }


    Could someone please explain what they have done here?
     
  2. jcsd
  3. Dec 20, 2011 #2

    Mark44

    Staff: Mentor

    When you post code, please surround your code with [code] and [/code] tags. This preserves indentation that might be there and makes your code easier to read.
    It's probably more useful for you to puzzle it out yourself. Take a simple string, like "my cat" and pretend you're the computer running your code. Do this with a sheet of paper, and keep track of the values of the loop control variable i and the other variable k. For each value of i, write down the value of the array and what happens to it. When the loop is complete you should see how the string has changed.
     
    Last edited: Jan 27, 2016
  4. Dec 20, 2011 #3

    D H

    User Avatar

    Staff: Mentor

    You need to get a different book. Your's is fatally flawed.

    I don't know if this code is in your text, for(i = 0 ; str !='0' ; i++) If it is it means the sample code is untested (very very bad). That line should be for(i = 0 ; str !='\0' ; i++)

    Using void main() is an unforgivable sin for a textbook on C++, as are using gets() and getch().


    You need to get a better book.
     
  5. Dec 20, 2011 #4
  6. Dec 20, 2011 #5
    Yeah, it is for(i=0 ; str != '/0' ; i++)
    Sorry that was a typo on my part.

    I do, however, agree that it is an extremely bad book. I use other books for concepts but I can't really help it when it comes to writing programs cause this book is provided by my education system.
     
  7. Dec 20, 2011 #6
    I know using gets( ) causes the program to crash since it does not let you specify a limit on how many characters are to be read
    But why is using getch( ) bad?
     
  8. Dec 20, 2011 #7

    D H

    User Avatar

    Staff: Mentor

    No, it doesn't. Given the input "testing 123", the given program with print "gnitset 321". Using std::reverse results in "321 gnitset".

    Besides, it's best if beginners at first avoid huge parts of the huge C++ library. Students need to learn how to iterate, how to search, how to write simple algorithms. Once they have learned such techniques they can be told "Oh, by the way. Almost all the stuff you've learned to write the last few weeks is already written for you in the C++ library."
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Similar Discussions: Reverse words of a string C++
  1. C++ strings help (Replies: 2)

  2. Strings in C (Replies: 6)

  3. Help with C++ strings (Replies: 3)

Loading...