Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to extract all words from a string in C++

2 Answers

0 votes
#include <iostream>
#include <sstream>
 
void extractAllWords(std::string str) {
    std::stringstream iss(str);
     
    std::string word;  
    while (iss >> word) {
        std::cout << word << '\n';
    }
}
  
  
int main() {
    std::string s = "c++ c c# java python";
      
    extractAllWords(s);
}
  
  
  
  
/*
run:
  
c++
c
c#
java
python
  
*/

 



answered Feb 8, 2022 by avibootz
edited Mar 14, 2024 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <sstream>

std::vector<std::string> extractWords(std::string str) {
    std::vector<std::string> words;
    std::istringstream ss(str);
 
    std::string word; 
 
    while (ss >> word)  {
        words.push_back(word);
    }
    
    return words;
}
 
int main() {
    std::string str = "C++ is a high-level general-purpose programming language";
    
    std::vector<std::string> words = extractWords(str);
    
    for (const std::string& w : words) {
        std::cout << w << std::endl;
    }
}



/*
run:

C++
is
a
high-level
general-purpose
programming
language

*/


 



answered Mar 14, 2024 by avibootz
...