【发布时间】:2020-03-12 01:14:14
【问题描述】:
如果我祈求从第 7 行开始解释以下代码,问会不会太过分?在大多数情况下,我很难理解 getchar() 与 gets() 或 scanf() 的不同之处。为什么添加一个转义序列很重要, \n 在我的信念中,值输入将以水平形式完成(因为它是一维字符串)?递增然后立即使用递减运算符也让我感到困惑。此外,需要有关 strcpy() 的帮助。 :| 如果有人有时间,我恳求指导。谢谢!
main()
{
char st[80], ch, word[15], lngst[15];
int i, l, j;
printf("\n Enter a sentence \n ");
i = 0;
while((st[i++] = getchar()) != '\n');
i--;
st[i] = '\0';
l = strlen(st);
i = 0; j = 0;
strcpy(word," ");
strcpy(lngst," ");
while(i <= l)
{
ch = st[i];
if(ch == ' ' || ch == '\0')
{
word[j] = '\0';
if(strlen(word) > strlen(lngst))
strcpy(lngst, word);
strcpy(word," ");
j = 0;
}
else
{
word[j] = ch;
j++;
}
i++;
}
printf("\n Longest word is %s", lngst);
printf("\n Press any key to continue...");
getch();
}
【问题讨论】:
-
注:function:
gets()已被贬值多年,并从最新版本的C标准中完全删除
标签: c while-loop strcpy getchar