题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
#include <stdio.h>
#include <string.h>
int main() {
char str[1001];
while (fgets(str,sizeof(str),stdin)) { // 注意 while 处理多个 case
char str_ans[1001];
memset(str_ans,0,sizeof(str_ans));
int j = 0;
int index = 0;
int count = 1;
int len = strlen(str);
str[len] = '\0';
for(int i = 0;i <= len;i++)
{
if((str[i] != ' '|| index%2 == 1) && str[i] != '"' )
{
str_ans[j++] = str[i];
}
else if(str[i] == '"')
{
index ++;//奇数表示忽略空格,偶数表示计算空格
}
else if((str[i] == ' ' || str[i] =='\0') && index %2 == 0)
{
//printf("%s\n",str_ans);
count++;
str_ans[j++] = '\n';
//memset(str_ans,0,sizeof(str_ans));
//j = 0;
}
}
//printf("%s\n", str);
printf("%d\n",count);
printf("%s\n", str_ans);
}
return 0;
}
也就轻轻松松,省事