关于C语言的一道简单题。

来源:百度知道 编辑:UC知道 时间:2024/05/12 15:09:52
输入一串字符串,输出这串字符串中最长的单词,要求使用函数。
我刚学C 能把具体的步骤写下来吗? 谢谢了。

#include<stdio.h>
# define SIZE 100
void findmax(char str[]);
main()
{
int i,j,max;
char str[SIZE];

printf("input the string('#' to end)\n");
gets(str);

printf("the longest word is:\n");
findmax(str);

system("pause");
return 0;

}
void findmax(char str[])
{
int i,j,max;
for(i=0;(str[i]!=' ')&&(str[i]!='.');i++);
max=i;
/*求第一个空格或 .或 #之前的字符的个数,默认它为最长的单词*/

/*第一个for嵌套,用于计算最长单词的长度,在这里不能打印最长单词是因为不循环结束
不能确定max的大小*/
for(i=0;str[i]!='#';i++)
{
for(j=0;str[i+j]!=' '&&str[i+j]!='.'&&str[i+j]!='#';j++);//j为每个单词的长度

if(j>max)
max=j;
}

/*第二个for嵌套,用于打印最长单词, 因为这时max已经确定*/
fo