Give a sentence, print different words present in it. Words are separated by space.
Examples:
Input : str = "Geeks for Geeks"
Output : Geeks
for
Geeks
Explanation : All space separated words
are printed line by line.
Input : str = "a computer science portal"
Output : a
computer
science
portal
Method 1 (Writing our own logic)
We traverse through all characters. If current character is space, we have reached end of a word, we print current word and reset it to empty. Else we append current character to word.
CPP
// C++ program to print words in a sentence#include <bits/stdc++.h>using namespace std;void removeDupWord(string str){ string word = ""; for (auto x : str) { if (x == ' ') { cout << word << endl; word = ""; } else { word = word + x; } } cout << word << endl;}// Driver codeint main(){ string str = "Geeks for Geeks"; removeDupWord(str); return 0;} |
Geeks for Geeks
Method 2 (Using strtok())
CPP
// C++ program to words in a sentence.#include <bits/stdc++.h>using namespace std;void removeDupWord(char str[]){ // Returns first token char *token = strtok(str, " "); // Keep printing tokens while one of the // delimiters present in str[]. while (token != NULL) { printf("%s\n", token); token = strtok(NULL, " "); }}// Driver codeint main(){ char str[] = "Geeks for Geeks"; removeDupWord(str); return 0;} |
Geeks for Geeks
Method 3 (Using stringstream)
CPP
// C++ program to print words in a sentence#include <bits/stdc++.h>using namespace std;void removeDupWord(string str){ // Used to split string around spaces. istringstream ss(str); string word; // for storing each word // Traverse through all words // while loop till we get // strings to store in string word while (ss >> word) { // print the read word cout << word << "\n"; }}// Driver codeint main(){ string str = "Geeks for Geeks"; removeDupWord(str); return 0;} |
Geeks for Geeks
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Frequency of smallest character in first sentence less than that of second sentence
- Remove all the palindromic words from the given sentence
- Java program to swap first and last characters of words in a sentence
- Count palindrome words in a sentence
- Count words that appear exactly two times in an array of words
- Check if the given string of words can be formed from words present in the dictionary
- Print given sentence into its equivalent ASCII form
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Sort an Array alphabetically when each number is converted into words
- Check if given string can be split into four distinct strings
- Split the string into substrings using delimiter
- Split the binary string into substrings with equal number of 0s and 1s
- Split the given string into Primes : Digit DP
- Split the given string into Odds: Digit DP
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Remove minimum characters from string to split it into three substrings under given constraints
- Count of ways to split given string into two non-empty palindromes
- Split the string into minimum parts such that each part is in the another string
- Check if a string can be split into two strings with same number of K-frequent characters
- Check if a string can be split into even length palindromic substrings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : abhiswaspatil1710

