c语言实现简单函数

来源:百度知道 编辑:UC知道 时间:2024/05/21 17:58:10
输入若干个单词,以空格分开,编写一个函数使其输出最长的单词并求该单词的长度,怎么写,若干个怎么表示?

#include "stdio.h"
#include "string.h"
int fun(char s[],char max[])
{
char ss[100][100],*p=s;
int i=0,j=0,k,n=0;
while(*p!='\0')
{
if((*p)!=' ')
{
ss[i][j]=*p;
j++;
}
else
{
ss[i][j]='\0';
i++;j=0;
}
p++;

}
ss[i][j]='\0';
strcpy(max,ss[0]);
n=n=strlen(max);
for(k=1;k<=i;k++)
{
if(strlen(ss[k])>strlen(max))
{
strcpy(max,ss[k]);
n=strlen(max);
}
}
return n;
}

int main()
{
char s[100];/*asdf asdfll asdf*/
char max[100];
int n;
gets(s);
n=fun(s,max);
printf("%s,%d",max,n);
}