字符串分割函数——Strtok() + cin.get()

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

char str[10000];
char delims[] = ",#";
char *result = NULL;
string  c[100];//等同于 char *c[100]

cin.get(str,10000);   //输入字符串(可带空格) 
//cout<<strlen(str)<<endl; 串的长度 

result = strtok( str, delims );
int cnt=0;
while( result != NULL )
{
	c[cnt++]=result;      //相当于指针传递 
	//printf( "result is \"%s\"\n", result );
	result = strtok( NULL, delims );
}

for(int i=0;i<cnt;i++)
 cout <<c[i]<<endl; 
 
//for(int i=0;i<cnt;i++)
//cout <<c[i][1];    可访问其中的任何一个字符 
	return 0;
}
// strtok(char *str,const char *delims) 以字符数组delims中的分隔符分割str中的字符串,
// 返回第一个分割完成后的字符串的指针

猜你喜欢

转载自blog.csdn.net/qq_41199502/article/details/88595834
今日推荐