用C ++将句子拆分成单词
将句子分解为单词的任务是给定的。在这里,我们将句子中出现的所有单词分开。
输入值
I am a good boy
输出结果
I am a good boy
在上面的示例中,我们将在一行中打印单个单词。
示例
#include<iostream.h>
#include<string.h>
#include<stdio.h>
Using namespace std;
void split( string st){
String word = “ “;
for ( char s : st){
If (s== ‘ ‘){
Cout<< z << endl;
z = “ “;
}
else{
z = z = s;
}
}
Cout<< z <<endl;
}
Int main( ){
String st = “ Blood from heaven is mortal”;
Cout<< “Blood from heaven is mortal”;
Split(st);
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出
Blood from heaven is mortal Blood From Heaven is mortal
示例
#include
#include
#include
Using namespace std;
Void split( string st){
String word = “ “;
for ( char s : st){
If (s== ‘ ‘){
Cout<< z << endl;
z = “ “;
}
else{
z = z = s;
}
}
Cout<< z <<
}
int main( ){
String st = “ type a text message”;
Cout<< “type a text message”;
Split(st);
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出
type a text message type a text message