Split the sentence into words in C++

C++Server Side ProgrammingProgramming

Given is the task to split the sentence into words. In this, we will separate all the words present in sentence.

Input 

I am a good boy

Output 

I
am
a
good
boy

In the above example we will print the single word in single line.

Example

#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;
}

Output

If we run the above code it will generate the following output

Blood from heaven is mortal
Blood
From
Heaven
is
mortal

Example

#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;
}

Output

If we run the above code it will generate the following output

type a text message
type
a
text
message
raja
Published on 14-Aug-2020 08:14:16
Advertisements