C++ String Exercises: Reverse the words in a string that have odd lengths
35. Reverse Words with Odd Lengths in a String
Write a C++ program to reverse all words that have odd lengths in a string.
Test Data:
("Exercises Practice Solution" ) -> "sesicrexE Practice Solution"
("The quick brown fox jumps over the lazy dog") -> "ehT kciuq nworb xof spmuj over eht lazy dog."
Sample Solution:
C++ Code:
Sample Output:
String: Exercises Practice Solution Reverse the words in the said string that have odd lengths: sesicrexE Practice Solution String: The quick brown fox jumps over the lazy dog. Reverse the words in the said string that have odd lengths: ehT kciuq nworb xof spmuj over eht lazy dog.
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C++ program to reverse only the words in a sentence that have an odd number of characters.
- Write a C++ program that processes a string and outputs a modified string where every word with an odd length is reversed.
- Write a C++ program to scan a sentence and reverse words of odd length using a loop and string reversal function.
- Write a C++ program that reads a sentence, reverses only odd-length words, and prints the updated sentence while preserving spaces.
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: Remove a word from a given string.
Next C++ Exercise: Check two consecutive, identical letters in a word.What is the difficulty level of this exercise?