如果同时有多个最长的单词,输出第一个。
#include<stdio.h>
#include <string.h>
void max(char a[])//函数求最长单词
{
int i,j;
int count = 0;
int max = 0;
char b[100];
int k;
for(i = 0; i <= strlen(a); i++)
{
if(a[i] != ' ' && a[i] !='\t' && a[i]!= '\0' )
count ++;
else if(a[i] == ' ' || a[i] == '\t' || a[i] == '\0')
{
if(max < count)
{
k=0;
max = count;
for(j = i-max; j < i; j++)
b[k++] = a[j];
b[k] = '\0';
}
count = 0;
}
}
printf("最长单词为:");
puts(b);
}
int main()
{
char a[80];
gets(a);
max(a);
}追问
#include <string.h>
void max(char a[])//函数求最长单词
{
int i,j;
int count = 0;
int max = 0;
char b[100];
int k;
for(i = 0; i <= strlen(a); i++)
{
if(a[i] != ' ' && a[i] !='\t' && a[i]!= '\0' )
count ++;
else if(a[i] == ' ' || a[i] == '\t' || a[i] == '\0')
{
if(max < count)
{
k=0;
max = count;
for(j = i-max; j < i; j++)
b[k++] = a[j];
b[k] = '\0';
}
count = 0;
}
}
printf("最长单词为:");
puts(b);
}
int main()
{
char a[80];
gets(a);
max(a);
}追问
本来只要together 逗号怎么办
温馨提示:答案为网友推荐,仅供参考