Lesson 1

Splitting and Rejoining Words in a String with C++


Hello, and welcome! Are you ready to elevate your string manipulation skills in C++? Today, we'll delve into a task that bolsters your comprehension of strings and enhances your creativity. The task involves splitting a string into words and then reversing each word as if reflected in a mirror. Does that sound interesting? Let's get started!

Task Statement and Description

You're tasked with considering a string filled with words and writing a C++ function that accepts this string. The function should reverse the character order of each word and form a new string consisting of these reversed words.

Here's what you need to keep in mind:

  • The input string will contain between 1 and 100 words.
  • Each word in the string is a sequence of characters separated by white space.
  • The characters can range from a to z, A to Z, 0 to 9, or even an underscore _.
  • The provided string will neither start nor end with a space, and double spaces won't be present either.
  • After reversing the words, your program should output a single string with the reversed words preserving their original order.


Consider the input string "Hello neat cpp_lovers_123".

The function works as follows:

  • 'Hello' becomes 'olleH'
  • 'neat' becomes 'taen'
  • 'cpp_lovers_123' becomes '321_srevol_ppc'

Afterward, it forms a single string with these reversed words, producing "olleH taen 321_srevol_ppc".

Therefore, if you call reverseWords("Hello neat cpp_lovers_123"), the function should return "olleH taen 321_srevol_ppc".

Let's begin breaking this down!

Step-by-Step Solution Building: Step 1

Our first task is to separate the words in the sentence. Unlike Python, C++ does not provide a built-in split() function. However, we can employ the string stream available in C++. A stringstream keeps a string internally, allowing us to extract words from the string. Here is a sample code to illustrate this:

1std::string input_str = "Hello neat cpp_lovers_123"; 2std::vector<std::string> words; 3 4std::istringstream iss(input_str); 5 6for(std::string s; iss >> s; ) 7 words.push_back(s); 8 9// Now the vector 'words' holds all the words of the string
Step-by-Step Solution Building: Step 2

Next, we need to reverse each word separated in the previous step. In C++, the std::reverse() function, found in the <algorithm> library, allows us to do this. Let's add these lines to our existing code:

1.... 2 3std::vector<std::string> reversed_words; 4 5for(auto word : words) 6{ 7 std::reverse(word.begin(), word.end()); 8 reversed_words.push_back(word); 9} 10 11// 'reversed_words' now contains the reversed words
Step-by-Step Solution Building: Step 3

Finally, we need to consolidate these reversed words into a single string, separated by spaces. We can achieve this using an ostringstream from the C++ Standard Library. Here's how we do that:

1std::ostringstream oss; 2oss << reversed_words[0]; // add first word 3 4for (int i = 1; i < reversed_words.size(); i++) 5 oss << " " << reversed_words[i]; // add the remaining words with space

First, we push the first word to the stream, then push a space followed by the next word in a loop. This way, we add all words while preserving the format. To get the resulting string, we can call oss.str().

Final Solution

It remains for us to combine the code from the steps together in a function reverseWords and call it from main to test.

1#include <sstream> //for std::istringstream and std::ostringstream 2#include <algorithm> //for std::reverse 3#include <vector> //for std::vector 4#include <iostream> //for std::cout and std::endl 5 6std::string reverseWords(std::string input_str) 7{ 8 std::vector<std::string> words; 9 std::istringstream iss(input_str); 10 11 for(std::string s; iss >> s; ) 12 words.push_back(s); 13 14 std::vector<std::string> reversed_words; 15 16 for(auto &word : words) 17 { 18 std::reverse(word.begin(), word.end()); 19 reversed_words.push_back(word); 20 } 21 22 std::ostringstream oss; 23 oss << reversed_words[0]; // add first word 24 25 for (int i = 1; i < reversed_words.size(); i++) 26 oss << " " << reversed_words[i]; // add the remaining words with space 27 28 return oss.str(); 29} 30 31int main() { 32 // Call the function 33 std::cout << reverseWords("Hello neat cpp_lovers_123") << std::endl; // prints: 'olleH taen 321_srevol_ppc' 34 return 0; 35}
Lesson Summary

Well done! By completing this lesson, you've sharpened your proficiency in manipulating strings in C++. You've especially improved in reversing the order of characters in a word. I hope you're feeling confident and excited about your C++ skills. Remember, mastering these skills requires frequent practice. Therefore, take some time to explore related problems and practice what you’ve learned. Enjoy the journey of learning!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.