Da una oración, imprime diferentes palabras presentes en ella. Las palabras están separadas por espacios.
Ejemplos:
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 code int main() { string str = "Geeks for Geeks"; removeDupWord(str); return 0; }
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 code int main() { char str[] = "Geeks for Geeks"; removeDupWord(str); return 0; }
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 code int main() { string str = "Geeks for Geeks"; removeDupWord(str); return 0; }