
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Split the sentence into words in C++
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
- Related Articles
- How to split sentence into blocks of fixed length without breaking words in JavaScript
- Reverse all the words of sentence JavaScript
- Rearrange Words in a Sentence in C++
- Count words in a sentence in Python program
- Count palindrome words in a sentence in C++
- Python program to count words in a sentence
- Counting number of words in a sentence in JavaScript
- Python program to sort Palindrome Words in a Sentence
- Arranging words by their length in a sentence in JavaScript
- Python program to sort out words of the sentence in ascending order
- Java program to sort words of sentence in ascending order
- Finding n most frequent words from a sentence in JavaScript
- Print all words occurring in a sentence exactly K times
- Python - Generate all possible permutations of words in a Sentence
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript

Advertisements