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 a split string in C++

3 Answers

0 votes
#include <iostream>

int main() {
 
    std::string str = "c++ c c# javascript python";

    std::string str1 = str.substr(9, 4); 
    std::cout << str1 << '\n';
    
    std::string str2 = str.substr(0, 5); 
    std::cout << str2 << '\n';
}
 
 
 
 
 
/*
run:
 
java
c++ c
 
*/

 



answered Feb 21, 2022 by avibootz
0 votes
#include <vector>
#include <iostream>
#include <sstream>
    
std::vector<std::string> &split(const std::string &s, 
                                char delimiter, 
                                std::vector<std::string> &vec) {
    std::stringstream ss(s);
    std::string str;
     
    while(std::getline(ss, str, delimiter)) {
        vec.push_back(str);
    }
     
    return vec;
}
 
void printVector(std::vector<std::string> const &v) {
    for (auto const &s: v) {
        std::cout << s << "\n";
    }
}
    
int main() {
    std::string str = "c++;php;java;c;python;c#;javascript;go";
     
    std::vector<std::string> vec;
     
    split(str, ';', vec);
     
    printVector(vec);
     
}
    
    
    
    
    
/*
run:
 
c++
php
java
c
python
c#
javascript
go
       
*/

 



answered Jun 28, 2023 by avibootz
edited Jun 28, 2023 by avibootz
0 votes
#include <iostream>
#include <sstream>

int main() {
    std::string str = "c++ php java c python c# javascript go";
    
    std::stringstream ss(str);  
    std::string word;
    
    while (ss >> word) { 
        std::cout << word << "\n";
    }
}
   
   
   
   
   
/*
run:

c++
php
java
c
python
c#
javascript
go
      
*/

 

 



answered Jun 28, 2023 by avibootz

Related questions

1 answer 97 views
1 answer 104 views
2 answers 99 views
1 answer 111 views
1 answer 74 views
1 answer 84 views
...