在 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

更新於: 14-Aug-2020

2K+ 瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.