มอบหมายให้แบ่งประโยคเป็นคำ ในนี้เราจะแยกคำทั้งหมดที่อยู่ในประโยค
ป้อนข้อมูล
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