Answers for "how to split string in c++ by space"

C++
1

cpp split string by space

std::vector<std::string> string_split(const std::string& str) {
	std::vector<std::string> result;
	std::istringstream iss(str);
	for (std::string s; iss >> s; )
		result.push_back(s);
	return result;
}
Posted by: Guest on March-01-2021
7

how to make string get spaces c++

string s;
getline(cin,s);
Posted by: Guest on June-02-2020
2

split a string based on a delimiter in c++

void tokenize(string &str, char delim, vector<string> &out)
{
	size_t start;
	size_t end = 0;

	while ((start = str.find_first_not_of(delim, end)) != string::npos)
	{
		end = str.find(delim, start);
		out.push_back(str.substr(start, end - start));
	}
}

int main()
{
    string s="a;b;c";
    char d=';';
    vector<string> a;
    tokenize(s,d,a);
    for(auto it:a)  cout<<it<<" ";

    return 0;
}
Posted by: Guest on July-21-2020
1

string split by space c++

// Extract the first token
char * token = strtok(string, " ");
// loop through the string to extract all other tokens
while( token != NULL ) {
  printf( " %sn", token ); //printing each token
  token = strtok(NULL, " ");
}
return 0;
Posted by: Guest on April-21-2021

Browse Popular Code Answers by Language