C 输入一串单词,找出最长的单词
void main(){
char s;
gets(s);
//¼ÆËã3¤¶è
int len=0;
while(s != '\0')
{
len++;
}
printf("%d\n",len);
int j=0;//¼Ç¼ÿ¸öμ¥′êμÄ3¤¶è
int n=0;//¼Ç¼éÏ′οոñμÄλÖÃ
char *t;//′æ·Åμ±Ç°×î3¤μÄμ¥′ê
int maxLen = 0; //′æ·Åμ±Ç°×î3¤μ¥′êμÄ3¤¶è
//¿aê¼
for(int i=0; i<len; i++)
{
//Õòμ½¿Õ¸ñ£¬±í꾿ոñÇ°ÃæμÄ×Ö·û′®Îaμ¥′꣬èç1ûêÇ×îoóò»¸ö×Öĸ ò2½øà′
if(s == ' ' || i==len-1)
{
if(i==len-1) //èç1ûêÇ×îoóò»¸ö×Öĸ
{
i++; //μ±Ç°μ¥′êμĽáêøμØ·½
j++;
}
char *s2 = new char;//±£′æμ±Ç°μ¥′ê
int k=n; //»ñμÃéÏ′ÎμÄ¿Õ¸ñλÖã¬ò2¾íêÇéϸöμ¥′ê×îoóμÄ¿Õ¸ñλÖÃ
int l=0; //Ñ-»·±äá¿
//¿aê¼Æ′½óμ¥′ê
while((k+l)<i)
{
s2 = s;
l++;
}
s2 = '\0'; //×Ö·û′®½áêø±êÖ¾
if(maxLen < j)//èç1ûéÏ′α£′æμÄ×î′óμÄ3¤¶è£¬D¡óúμ±Ç°μ¥′êμÄ3¤¶è£¬¾íìæ»»
{
maxLen = j;
t = s2;
}
j = 0; //Çå¿Õ3¤¶è
n = i+1; //¼Ç¼±¾′οոñλÖã¬oÃèÃÏÂ′ÎÑ-»·
}else
{
j++;//¼ÆËãμ±Ç°μ¥′êμÄ3¤¶è
}
}
printf("×î3¤μÄμ¥′êÎa:%s\n3¤¶èÎa:%d",t,maxLen);
}
页:
[1]