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

  • Context: Comp Sci 
  • Thread starter Thread starter Ilikecereal
  • Start date Start date
  • Tags Tags
    C++ Reverse String
Click For Summary

Discussion Overview

The discussion centers around a C++ program designed to reverse individual words within a string. Participants express confusion regarding the logic of the provided code and critique its implementation, while also discussing best practices in C++ programming.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests clarification on the logic of the provided C++ code, which aims to reverse words in a string.
  • Another participant suggests that understanding the code can be achieved by manually tracing its execution with a simple example.
  • A critique is made regarding the use of the condition in the for loop, pointing out a potential error in the termination condition of the loop.
  • Concerns are raised about the use of 'void main()' and functions like 'gets()' and 'getch()', which are deemed inappropriate for C++ programming.
  • Some participants mention that C++ has built-in functions for reversing sequences, but there is disagreement on their applicability to the specific problem of reversing individual words.
  • One participant argues that beginners should focus on learning fundamental programming techniques rather than relying on library functions too early.

Areas of Agreement / Disagreement

Participants express disagreement regarding the quality of the textbook and the appropriateness of certain coding practices. There is no consensus on the best approach to teaching C++ programming or the use of built-in functions versus manual coding techniques.

Contextual Notes

There are unresolved issues regarding the correctness of the code, particularly concerning the loop condition and the use of certain functions. Participants also highlight the potential risks associated with using 'gets()' without character limits.

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] ;
count<<"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)
{ count<<word[--k] ;
}
count<<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   Reactions: 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   Reactions: 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   Reactions: 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."
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
3K
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K