输入若干字符串(不超过10个),以stop作为最后字符串,输出最长字符串

//输入若干字符串,以stop作为最后字符串,输出最长字符串 
//这里出现的一个常见错误是,我们没有很好的运用gets函数和strcmp函数;
//scanf函数输入字符串就要将字符数组设置为char*,但是不如用gets函数更方便一些;
#include<stdio.h>
#include<string.h>
int main()
{
	char a[11][100];
	int max=strlen("stop"); //首先计算stop的长度 
	int s;   //串的长度 
	int i=0;
	int c;  //存放最长串的位置 
	gets(a[i]);
	while(strcmp(a[i],"stop")&&i<10)
	{
		s=strlen(a[i]);    //此函数可以计算字符串长度 
		if(s>max)         //求最长传唱度 
		{
			max=s;
			c=i;            //找出最长串的位置 
		}

		i++;
		if(i==9)
		{
			printf("请输入:stop:\n");
			continue;
		}
		gets(a[i]);

	}
	if(max>4)    //当没有字符串长度大于stop时,输出stop
	{
		printf("%s",a[c]); 
	}
	else
	{
		printf("%s","stop"); 
	}
//	printf("%s",a[c]); 
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_40179546/article/details/80553537