Comp Sci How Does This C++ Program Reverse Individual Words in a String?

AI Thread Summary
The discussion focuses on a C++ program designed to reverse individual words in a string, with the example input "I love C++" producing "I evol ++C". Participants express confusion over the logic of the provided code and highlight several issues, including the use of outdated functions like `gets()` and `getch()`, and a typo in the loop condition. They suggest that the textbook is flawed and recommend using better resources for learning C++. Additionally, there is a debate about the appropriateness of using built-in functions versus learning fundamental programming techniques. Overall, the conversation emphasizes the importance of understanding basic algorithms before relying on library functions.
Ilikecereal
Messages
17
Reaction score
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?
 
Physics news on Phys.org
When you post code, please surround your code with
Code:
 and
tags. This preserves indentation that might be there and makes your code easier to read.
Ilikecereal said:
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.
Code:
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.
 
Last edited:
  • Like
Likes loophole
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.
 
  • Like
Likes loophole
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.
 
  • Like
Likes loophole
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?
 
bigfooted said:
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."
 
Back
Top