JiaHeng-DLUT
9/14/2019 - 10:42 AM

How to split a string❓

#include <stdio.h>
#include <string.h>

int main() {
	char str[80] = "A BC DEF GHIJ";
	const char s[2] = " ";
	char* token;

	// get the first substring
	token = strtok(str, s);

	// get other substrings
	while (token != NULL) {
		printf("%s\n", token);
		token = strtok(NULL, s);
	}
	//A
	//BC
	//DEF
	//GHIJ
	return(0);
}
#include <iostream>
#include <string>
using namespace std;

int main() {
	string s = "Yesterday you said tomorrow.";
	s += ' ';
	while (1) {
		int pos = s.find(' ');
		cout << s.substr(0, pos) << endl;
		if (pos == string::npos) {
			break;
		}
		s = s.substr(pos + 1);
	}
	//Yesterday
	//you
	//said
	//tomorrow.
	return 0;
}