How to split a string into words in C++

3 Answers

0 votes
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>

using namespace std;

int main()
{
	char s[] = "C++ C C# Java PHP";

	vector<string> tokens;

	istringstream iss(s);
	copy(istream_iterator<string>(iss),
		istream_iterator<string>(),
		back_inserter(tokens));

	for (auto word : tokens) {
		cout << word << endl;
	}

	return 0;
}


/*
run:

C++
C
C#
Java
PHP

*/

 



answered Feb 4, 2017 by avibootz
0 votes
#include <iostream>

using namespace std;

int main()
{
	char s[] = "C++ C C# Java PHP";
	char *p;

	p = strtok(s, " ");
	while (p != NULL)
	{
		cout << p << endl;
		p = strtok(NULL, " ");
	}

	return 0;
}


/*
run:

C++
C
C#
Java
PHP

*/

 



answered Feb 4, 2017 by avibootz
0 votes
#include <iostream>

using namespace std;

int main()
{
	char s[] = "-c, c++.. c# -java ,php...python";
	char *p;

	p = strtok(s, " -.,");
	while (p != NULL)
	{
		cout << p << endl;
		p = strtok(NULL, " -.,");
	}

	return 0;
}


/*
run:

c
c++
c#
java
php
python

*/

 



answered Feb 4, 2017 by avibootz

Related questions

1 answer 145 views
1 answer 207 views
207 views asked Dec 25, 2021 by avibootz
1 answer 150 views
3 answers 247 views
1 answer 150 views
1 answer 167 views
167 views asked Dec 25, 2021 by avibootz
...