题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
#include <string.h>
//fgets获取后用top+ wordlen确定参数长度,截取后给参数,最多四个参数
int main()
{
char str[1000] = {'\0'};
fgets(str, 1000, stdin);
int wordlen = 0;
char parameter[100][1000] = {'\0'};
int top = 0;
for (int i = 0; i < strlen(str); i++)
{
if(str[i]=='"'){
while(str[++i]!='"'){
wordlen++;
}
strncpy(parameter[top++], str + i - wordlen, wordlen);
wordlen = 0;
i++;
if(str[i]==' '){}
}
else if (str[i] == ' '||str[i] == '\n')
{
strncpy(parameter[top++], str + i - wordlen, wordlen);
wordlen = 0;
}
else
{
wordlen++;
}
}
printf("%d\n",top);
for(int i=0;i<top;i++)
printf("%s\n",parameter[i]);
return 0;
}