题:输入一个字符串,字符串中单次以空格隔开,依次打印所有单词
例输入:Hello Hello world!
输出:Hello
Hello
worle!
代码:
#include <iostream>
using namespace std;
#include <string>
int main()
{
char str[100];
gets_s(str, 100);
int n = strlen(str);
for (int i = 0; i < n; i++) {
int j = i;
while (j < n && str[j] != ' ')j++;
for (int k = i; k < j; k++) {
printf("%c", str[k]);
}
printf("\r\n");
i = j;
}
return 0;
}
运行结果: