Reverse words of a string C++

Until then, it's better to teach a student how to write a loop that reverses the order of a string themselves, so that they understand why std::reverse does what it does.
  • #1
Ilikecereal
17
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
  • #2
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.
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
  • #3
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
  • #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.
 
  • Like
Likes loophole
  • #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?
 
  • #7
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."
 

1. What is the purpose of reversing words in a string in C++?

The purpose of reversing words in a string in C++ is to change the order of the words in a string, for example from "Hello World" to "World Hello". This can be useful in various applications such as data encryption, text processing, or for reversing the output of a program.

2. How can I reverse the words of a string in C++?

To reverse the words of a string in C++, you can use the reverse() function from the algorithm library. This function takes two iterators, indicating the beginning and end of the range to be reversed, and swaps the elements in that range. You can also use a for loop to iterate through the string and reverse the words manually.

3. Can I reverse the words of a string in place in C++?

Yes, it is possible to reverse the words of a string in place in C++. This means that the original string will be modified instead of creating a new string with the reversed words. To do this, you can use a combination of the reverse() function and the reverse_iterator class. This allows you to iterate through the string in reverse and modify the elements in place.

4. Is it possible to reverse only certain words in a string in C++?

Yes, it is possible to reverse only certain words in a string in C++. This can be achieved by using the find() function to locate the specific words that you want to reverse, and then using the reverse() function on that portion of the string. You can also use a substring() function to isolate the words that you want to reverse and then use the reverse() function on that substring.

5. Are there any limitations to reversing words in a string in C++?

There are some limitations to reversing words in a string in C++. One limitation is that the words must be separated by a space or some other delimiter in order for the function to work properly. Also, if the string contains special characters or punctuation, it may affect the output of the reversed words. Additionally, the reverse() function may not work on strings that are too long, as it may cause memory overflow. It is important to take these limitations into consideration when using the reverse() function to reverse words in a string in C++.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
3
Views
749
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
Back
Top