Problem Statement
Take a sentence as input from the user and split it into words, printing each word on a new line.
Concepts and Explanation
This program takes a sentence as input from the user and splits it into words, printing each word on a new line.
Header files: #include <iostream>
, #include <string>
The iostream
header file is part of the C++ standard library, providing functionality for input and output streams. The string
header file provides classes and functions for working with strings. In this program, we use both of these header files.
Using namespace std
The using namespace std;
directive is used to avoid typing std::
before every standard library object, function, or type. This allows us to write cout
instead of std::cout
, for example. This is a convenient way to reduce code verbosity.
Main Function
The main()
function is the entry point of the program, where the code execution starts. It has a return type of int
, and in this case, it takes no arguments.
Variables
We declare a string
variable named input
to store the user input.
Console Input and Output
We use cout
to print a message to the console, asking the user to enter a sentence. The getline()
function is used to read a line of input from the user and store it in the input
variable.
Splitting the String
We create a stringstream
object named ss
and pass it the input
string. We then use a while
loop to repeatedly extract tokens from the stringstream
object using the >>
operator, which extracts whitespace-separated strings from the stream. For each token extracted, we simply print it to the console.
Logic of the Program
The main logic of the program is to take a sentence as input from the user and split it into words. To achieve this, the program follows these steps:
- Print a message asking the user to enter a sentence.
- Read the sentence input by the user using the
getline()
function. - Create a
stringstream
object and pass it the input string. - Use a
while
loop to repeatedly extract tokens from thestringstream
object using the>>
operator. - For each token extracted, print it to the console on a new line.
- Once all tokens have been extracted and printed, the program ends.
By following this logic, the program can efficiently split a sentence into words and print them to the console.
Ending the Program
Once all tokens have been extracted and printed, the program ends by returning 0
, indicating that the program has executed successfully.
Method 1
/*
* -----------------------------------------------------------
* OOPS using C++ Language (CSU1287P)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string input;
cout << "This program helps you print each word from the entered sentence into new lines, using object-oriented programming concepts and algorithms in C++.";
cout << "Enter a sentence - (Example: I live in India) - and press Enter: ";
getline(cin, input);
stringstream ss(input);
string token;
while (ss >> token) {
cout << token << endl;
}
return 0;
}
Method 2
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
string delimiter = " ";
size_t pos = 0;
string token;
while ((pos = input.find(delimiter)) != string::npos) {
token = input.substr(0, pos);
cout << token << endl;
input.erase(0, pos + delimiter.length());
}
cout << input << endl;
return 0;
}
Method 3: Using the strtok() function from the C standard library
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char input[100];
cout << "Enter a sentence: ";
cin.getline(input, 100);
char* token = strtok(input, " ");
while (token != NULL) {
cout << token << endl;
token = strtok(NULL, " ");
}
return 0;
}
Method 4: Using a regular expression to split the string
#include <iostream>
#include <regex>
using namespace std;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
regex reg("\\s+");
sregex_token_iterator iter(input.begin(), input.end(), reg, -1);
sregex_token_iterator end;
while (iter != end) {
cout << *iter++ << endl;
}
return 0;
}
Method 5: Using the Boost Tokenizer library
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
char_separator sep(" ");
tokenizer> tokens(input, sep);
for (const auto& token : tokens) {
cout << token << endl;
}
return 0;
}
Method 6: Using default library iostream
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
string word = "";
for (char c : input) {
if (c == ' ') {
cout << word << endl;
word = "";
} else {
word += c;
}
}
cout << word << endl;
return 0;
}
Method 7: Using string input.substr()
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter a sentence: ";
getline(cin, input);
int start = 0;
for (int i = 0; i < input.size(); i++) {
if (input[i] == ' ') {
cout << input.substr(start, i - start) << endl;
start = i + 1;
}
}
cout << input.substr(start) << endl;
return 0;
}
Method 8: Using istringstream and getline()
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string sentence;
std::getline(std::cin, sentence);
std::istringstream ss(sentence);
std::string word;
while (ss >> word) {
std::cout << word << "\n";
}
return 0;
}
Method 9: Using strtok() and cstring
#include <iostream>
#include <cstring>
int main() {
char sentence[100];
std::cin.getline(sentence, 100);
char* token = std::strtok(sentence, " ");
while (token != nullptr) {
std::cout << token << "\n";
token = std::strtok(nullptr, " ");
}
return 0;
}
Output for Method 1:
This program helps you print each word from the entered sentence into new lines, using object-oriented programming concepts and algorithms in C++.
Enter a sentence - (Example: I live in India) - and press Enter: I am dmjone
I
am
dmjone