#include<stdio.h>
int Longest(char *, int *);
int Longest(char str[], int *t)
{
char ch;
int max = 0, c = 0, place, word = 0,i = 0, local, temp;
*t = temp = 0;
for(; str[i] ; i++)
{
ch = str[i];
if(ch == ' ')
{
word = 1;
if(max < c)
{
local = place;
max = c;
*t = temp;
}
}
else if(ch >= 'a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(word == 1)
{
c = 1;
place = i;
word = 0;
temp++;
}
else c++;
}
}
if(max < c)
{
local = place;
max = c;
*t = temp;
}
return local;
}
int main()
{
char s[] = " rhkiilabc budf jkshd lsielid ";
int i, j;
i = Longest(s, &j);
printf("%s\n",s);
printf("最长的单词在第%d个\n", j);
for(;s[i]!= ' '&&s[i] != '\0';i++)
printf("%c",s[i]);
return 0;
}
C语言 输出最长的单词
最新推荐文章于 2023-09-26 23:51:35 发布