Give a sentence, print different words present in it. Words are separated by space.
Examples:
Input : str = "Geeks for Geeks"
Output : Geeks
for
Geeks
Explanation : All space separated words
are printed line by line.
Input : str = "a computer science portal"
Output : a
computer
science
portal
Method 1 (Writing our own logic):
We traverse through all characters. If current character is space, we have reached end of a word, we print current word and reset it to empty. Else we append current character to word.
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
void removeDupWord(string str)
{
string word = "";
for (auto x : str)
{
if (x == ' ')
{
cout << word << endl;
word = "";
}
else {
word = word + x;
}
}
cout << word << endl;
}
int main()
{
string str = "Geeks for Geeks";
removeDupWord(str);
return 0;
}
|
Complexity Analysis:
- Time complexity : O(n)
- Auxiliary Space : O(n)
Method 2 (Using strtok()):
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
void removeDupWord(char str[])
{
char *token = strtok(str, " ");
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, " ");
}
}
int main()
{
char str[] = "Geeks for Geeks";
removeDupWord(str);
return 0;
}
|
Note: strtok() function cannot be used with C++ STL string. It requires string to be character array.
Complexity Analysis:
- Time complexity : O(n)
- Auxiliary Space : O(n)
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
void removeDupWord(string str)
{
istringstream ss(str);
string word;
while (ss >> word)
{
cout << word << "\n";
}
}
int main()
{
string str = "Geeks for Geeks";
removeDupWord(str);
return 0;
}
|
Time complexity : O(n)
Auxiliary Space : O(n)
The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header.
Implementation
C++
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string sentence = "Geeks For Geeks";
std::string word;
std::istringstream iss(sentence);
while (std::getline(iss, word, ' ')) {
std::cout << word << std::endl;
}
return 0;
}
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1).
Method 5 (Using Temporary String):
In C++, one approach is to use a temporary string to hold each word as it is extracted from the sentence. The sentence can be split into words by iterating over each character in the sentence and checking for whitespace characters. When a whitespace character is encountered, the temporary string is considered a word and can be added to a list of words. This process is repeated until all characters in the sentence have been processed.
Implementation
C++
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string sentence = "Geeks For Geeks";
std::vector<std::string> words;
std::string tempWord;
for (char c : sentence) {
if (isspace(c)) {
if (!tempWord.empty()) {
words.push_back(tempWord);
tempWord.clear();
}
} else {
tempWord.push_back(c);
}
}
if (!tempWord.empty()) {
words.push_back(tempWord);
}
for (std::string word : words) {
std::cout << word << std::endl;
}
return 0;
}
|
Time Complexity: O(N), where N is the length of the sentence.
Auxiliary Space: O(N)
Method 6 (Using find(), substr() and erase() Functions):
One approach is to use the find(), substr() and erase() functions to split a sentence into words in C++.
Steps:
- Initialises a string with the sentence and declares two variables for the position and the extracted word.
- It then uses a while loop to find the spaces in the sentence and extract the words using the substr() function.
- Lastly, it prints out the last word of the sentence.
Implementation
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sentence = "Geeks For Geeks";
size_t pos = 0;
string word;
while ((pos = sentence.find(" ")) != string::npos) {
word = sentence.substr(0, pos);
cout << word << endl;
sentence.erase(0, pos + 1);
}
cout << sentence;
return 0;
}
|
Time Complexity: O(N), where N is the length of the sentence.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!