c语言,如何读取逗号分隔的字符串,将逗号间的字符串分别提取出来。 100
例如:我输入312321,312231,54,23324,3,a,s(s后没有逗号)我希望得到一个int性的数组第0到第6号位置分别是上面逗号之间的数字与字符程序运行之前...
例如:我输入312321,312231,54,23324,3,a,s(s后没有逗号)
我希望得到一个int性的数组第0到第6号位置分别是上面逗号之间的数字与字符
程序运行之前并不知道会输入多长的字符串。
谢谢
第二行“int性”应该是“int型” 展开
我希望得到一个int性的数组第0到第6号位置分别是上面逗号之间的数字与字符
程序运行之前并不知道会输入多长的字符串。
谢谢
第二行“int性”应该是“int型” 展开
展开全部
先将所有的读进来存在一个字符串中,然后用字符分割函数strtok()//具体可参见API
例如:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
以上代码的运行结果是:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
例如:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
以上代码的运行结果是:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询