दिया गया कार्य वाक्य को शब्दों में विभाजित करना है। इसमें, हम वाक्य में मौजूद सभी शब्दों को अलग करेंगे।
इनपुट
I am a good boy
आउटपुट
I am a good boy
उपरोक्त उदाहरण में हम प्रत्येक शब्द को अलग लाइन में प्रिंट करेंगे।
उदाहरण
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void split(string str) {
stringstream ss(str);
string word;
while (ss >> word) {
cout << word << endl;
}
}
int main() {
string st = "Blood from heaven is mortal";
cout << "Original: " << st << endl;
cout << "Split words:" << endl;
split(st);
return 0;
}
आउटपुट
Original: Blood from heaven is mortal Split words: Blood from heaven is mortal
उदाहरण
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void split(string str) {
stringstream ss(str);
string word;
while (ss >> word) {
cout << word << endl;
}
}
int main() {
string st = "type a text message";
cout << "Original: " << st << endl;
cout << "Split words:" << endl;
split(st);
return 0;
}
आउटपुट
Original: type a text message Split words: type a text message