题目描述:
给定由若干个单词组成的字符串,字符串的长度小于 500,单词和单词之间由空格分开,求出其中的最长最短单词
输入描述:
长度小于 500 的字符串
输出描述:
输出为 2 行,每行一个单词,第一行输出最长单词,第二行输出最短单词,若有多个相同长度的最长最短单词,则输出第一次出现的那个
样例输入:
I play the leading man who else
样例输出:
leading
I
#include<iostream>
using namespace std;
int main()
{
char a[500];
int i=0,j=0,b=0,m=0,n=0,d=0;
gets(a);
while(a[i]!=' ')
{
while(a[i]!=' '&&a[i]!=' ')
{
j++;
i++;
}
if(j>b)
{
m=i-j;
b=j;
}
if(j<d)
{
n=i-j;
d=j;
}
while(a[i]==' ')
i++;
j=0;
}
while(a[m]!=' '&&a[m]!=' ')
{
cout<<a[m];
m++;
}
cout<<endl;
while(a[n]!=' '&&a[n]!=' ')
{
cout<<a[n];
n++;
}
cout<<endl;
return 0;
}