【C系列5.4】指针专题之分割字符串(strtok与gets的应用)(hznuoj)
Description
Alex的好朋友都去生猴子了,所以她只好百无聊赖地继续玩字符串游戏。输入一个长度不超过10000的字符串,字符串中只含字母和空格,空格用于分隔单词,请将字符串中用空格分隔的单词输出来。
Input
输入含多组测试数据,每组占一行,是一个长度不超过10000的字符串,只含字母和空格。
Output
将字符串中用空格分隔的单词输出来,每个单词一行。
每组测试数据之间用空行隔开。
Samples
input
Hello world
output
Hello world
#include<stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int main(void)
{
int i, count = 0;
char str[10000], * token;
const char s[2] = " ";
while (fgets(str, sizeof(str), stdin) != NULL) {
token = strtok(str, s);
while (token != NULL) {
if (strlen(token) > 0) {
count++;
printf("%s\n", token);
}
token = strtok(NULL, s);
}
}
return 0;
}

浙公网安备 33010602011771号