Write a C++ program to split a given sentence into words. Words are separated by whitespace.
Examples:
Input: "Geeks for Geeks"
Output: "Geeks",
"for",
"Geeks"
Explanation: The sentence is split into words wherever there is a space.
Input: "C++ is fun"
Output: "C++",
"is",
"fun"
Explanation: The sentence is split into words at each space, resulting in three separate words.
How to Split String into Words in C++?
Splitting a sentence into words involves breaking down a sentence into individual words separated by spaces. This process is also called tokenization of the string. C++ provides many different methods to split the given sentence string into words. Some of the most commonly used ones are:
Table of Content
1. Using stringstream
The idea is to use the stringstream class to create a stream to the given string. Then use cin to extract words one by one from the mentioned stream. cin uses the whitespace character as a delimiter(seperator) by default.
Implementation
// C++ Program to Split a Sentence into Words using stringstream
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
vector<string> split_sentence(string sen) {
// Create a stringstream object
stringstream ss(sen);
// Variable to hold each word
string word;
// Vector to store the words
vector<string> words;
// Extract words from the sentence
while (ss >> word) {
// Add the word to the vector
words.push_back(word);
}
return words;
}
int main() {
string sen = "Geeks for Geeks";
// Call the function to split the sentence
vector<string> words = split_sentence(sen);
for (string w : words) {
cout << w << endl;
}
return 0;
}
Output
Geeks for Geeks
Splitting a sentence into words is a common text-processing task.
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
2. Using getline with a Custom Delimiter
We can use the getline() function to extract words by specifying the whitespace character as the delimiter instead of cin. We still need to create a stringstream object to the given string.
Implementation
// C++ Program to Split a Sentence into Words using getline
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
vector<string> split_sentence(string sen) {
// Create a stringstream object
stringstream ss(sen);
// Variable to hold each word
string word;
// Vector to store the words
vector<string> words;
// Extract words using getline with space as the delimiter
while (getline(ss, word, ' ')) {
// Add the word to the vector
words.push_back(word);
}
return words;
}
int main() {
string sen = "Geeks for Geeks";
vector<string> words = split_sentence(sen);
for (string w : words) {
cout << w << endl;
}
return 0;
}
Output
Geeks for Geeks
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
3. Using find() and substr() Functions
We can also use the find() and substr() functions to manually search for spaces and extract words. We can use find() function to find the index of the next whitespace. Then we can use the substr() function to extract the word between the current and previous whitespace.
Implementation
// C++ Program to Split a Sentence into Words using find and substr
#include <iostream>
#include <vector>
using namespace std;
vector<string> split_sentence(string sen) {
// Vector to store the words
vector<string> words;
// Initial index position
size_t start = 0;
// Variable to store the position of space
size_t end;
// Loop to find and extract words
while ((end = sen.find(' ', start)) != string::npos) {
// Extract the word using substr
words.push_back(sen.substr(start, end - start));
// Update the starting index
start = end + 1;
}
// Add the last word to the vector
words.push_back(sen.substr(start));
return words;
}
int main() {
string sen = "Geeks for Geeks";
// Call the function to split the sentence
vector<string> words = split_sentence(sen);
for (string w : words) {
cout << w << endl;
}
return 0;
}
Output
Geeks for Geeks
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
4. Manually Extracting Words Using Loop
It is also pretty easy to extract the words manually by detecting the spaces while iterating through the given sentence string.
Below is the approach to manually extract words using loop:
- Initialize an temporary string to hold each word.
- Loop through each character in the sentence.
- Append characters to the temporary string until a space is encountered.
- When a space is found, store the temporary string in a vector and reset it.
- Keep doing it till the end of the string.
Implementation
// C++ Program to Split a Sentence into Words using
// Manual Extraction
#include <iostream>
#include <vector>
using namespace std;
vector<string> split_sentence(string sen) {
// Vector to store the words
vector<string> words;
// Temporary string to hold each word
string word = "";
// Iterate through each character in the sentence
for (char c : sen) {
if (c == ' ') {
// If a space is found, add the word to the vector
words.push_back(word);
// Reset the word
word = "";
}
else {
// Append the character to the current word
word += c;
}
}
// Add the last word to the vector
if (!word.empty()) {
words.push_back(word);
}
// Return the vector containing words
return words;
}
int main() {
string sen = "Geeks for Geeks";
// Call the function to split the sentence
vector<string> words = split_sentence(sen);
for (string w : words) {
cout << w << endl;
}
return 0;
}
Output
Geeks for Geeks
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
5. Using strtok Function
The strtok() function is a C function that is used to split the sentence into words based on spaces, but this function only works on the C-Style strings i.e. character arrays terminated by NULL character.
Syntax of strtok()
strtok(str, delims);It returns the pointer to the first token encountered in the string, NULL if there are no more tokens found.
Implementation
// C++ Program to Split a Sentence into Words using strtok
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
vector<string> split_sentence(char *sen) {
// Vector to store the words
vector<string> words;
const char delimiter[] = " ";
// Use strtok to split the sentence into words
char *word = strtok(sen, delimiter);
// Loop through each word
while (word != nullptr) {
// Add the current word to the vector
words.push_back(word);
// Get the next word using strtok
word = strtok(nullptr, delimiter);
}
// Return the vector containing words
return words;
}
int main() {
char sen[] = "Geeks for Geeks";
// Call the function to split the sentence
vector<string> words = split_sentence(sen);
for (string w : words) {
cout << w << endl;
}
return 0;
}
Output
Geeks for Geeks
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)