我希望能够扫描具有这种格式的字符串
"hello world !!!!"
到
{"hello world", "!!!!"}
这 2 个字符串之间用 1 个以上的空格分隔。我可以解析这个或至少检测 scanf 中的 2 个连续空格吗?
最佳答案
这段代码可以帮到你
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char a_buf[5][100] = {0}, sep[3] ,*buf = a_buf[0];
int i = 0;
buf = a_buf[0];
while (scanf("%s%2[ ]",buf,sep) > 1) {
if (strlen(sep)==1)
{
buf += strlen(buf);
*buf++ = ' ';
}
else
buf = a_buf[++i];
}
}
关于c - 两个空格作为 scanf 分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14334505/