Given là nhiệm vụ tách câu thành các từ. Trong phần này, chúng ta sẽ tách tất cả các từ có trong câu.
Đầu vào
I am a good boy
Đầu ra
I am a good boy
Trong ví dụ trên, chúng ta sẽ in từ đơn trong một dòng.
Ví dụ
#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;
} Đầu ra
Nếu chúng ta chạy đoạn mã trên, nó sẽ tạo ra kết quả sau
Blood from heaven is mortal Blood From Heaven is mortal
Ví dụ
#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;
} Đầu ra
Nếu chúng ta chạy đoạn mã trên, nó sẽ tạo ra kết quả sau
type a text message type a text message