Avatar ·

How to iterate over words in a string in C++: string tokenization

Hello everyone! I'm new to C++ (previously wrote in Python) and I've encountered a task that seems elementary but has me stumped. I need to split a string into individual words and iterate over them in a loop. In Python, this is done in one line: `for word in sentence.split():`. But in C++, everything turned out to be more complicated. Here's an example of my task. There is a string with a sentence: ```cpp std::string text = "Hello, world! This is a test."; ``` I need to get each word separately: "Hello,", "world!", "This", "is", "a", "test." (or without punctuation marks — that's already details). I tried using `std::stringstream`: ```cpp std::stringstream ss(text); std::string word; while (ss >> word) { std::cout << word << std::endl; } ``` This works, but only if words are separated by spaces. What about