Reverse words of a string C++


by Ilikecereal
Tags: reverse, string, words
Ilikecereal
Ilikecereal is offline
#1
Dec20-11, 10:52 AM
P: 15
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[i] !='0' ; i++)
{
if(str[i] != ' ')
{ word[k] = str[i] ;
k = k + 1 ;
}
else
{
while(k > 0)
{ cout<<word[--k] ;
}
cout<<str[i] ;
}
}
getch( ) ;
}


Could someone please explain what they have done here?
Phys.Org News Partner Science news on Phys.org
Review: With Galaxy S5, Samsung proves less can be more
Making graphene in your kitchen
Study casts doubt on climate benefit of biofuels from corn residue
Mark44
Mark44 is offline
#2
Dec20-11, 12:46 PM
Mentor
P: 21,032
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.
Quote Quote by Ilikecereal View Post
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[i] !='0' ; i++)
  {  
      if(str[i] != ' ')
      { 
          word[k] = str[i] ;
          k = k + 1 ;
      }
      else
      {  
         while(k > 0)
         { cout<<word[--k] ;
         }
         cout<<str[i] ;
      }
   }
   getch( ) ;
}
Could someone please explain what they have done here?
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.
D H
D H is offline
#3
Dec20-11, 01:22 PM
Mentor
P: 14,459
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[i] !='0' ; i++) If it is it means the sample code is untested (very very bad). That line should be for(i = 0 ; str[i] !='\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.

bigfooted
bigfooted is offline
#4
Dec20-11, 01:29 PM
P: 263

Reverse words of a string C++


Also, C++ has built-in functions to do all this for you, e.g.

http://www.cplusplus.com/reference/algorithm/reverse/
Ilikecereal
Ilikecereal is offline
#5
Dec20-11, 01:33 PM
P: 15
Yeah, it is for(i=0 ; str[i] != '/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.
Ilikecereal
Ilikecereal is offline
#6
Dec20-11, 01:39 PM
P: 15
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?
D H
D H is offline
#7
Dec20-11, 01:47 PM
Mentor
P: 14,459
Quote Quote by bigfooted View Post
Also, C++ has built-in functions to do all this for you, e.g.

http://www.cplusplus.com/reference/algorithm/reverse/
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."


Register to reply

Related Discussions
The 1st 3 words... Fun, Photos & Games 27
"to the nearest order of magnitude." General Physics 6
how many words... Set Theory, Logic, Probability, Statistics 5
How many words General Discussion 27
Words Forum Feedback & Announcements 3