#include<stdio.h>
#include <stdlib.h>
#include<string.h>
void maxLenStr(char* str[], int n, int* max)
{
//从字符串数组str中找出最长的一个字符串,并将其下标存入形参指针max所指内存。
int Max = 0;//下标
int i = 0;
int len_str,len_max;
len_max = strlen(str[0]);
for (i = 0; i < n; i++)
{
len_str = strlen(str[i]);
if (len_str > len_max)
{
len_max = len_str;
Max = i;
}
}
*max = Max;
}
int main()
{
char* str[100];
char s[80];
int i;
char* max = (int*)malloc(sizeof(int));
for (i = 0;; i++)
{
gets(s);
if (strcmp(s, "****") == 0)
{
break;
}
else
{
str[i]= (char*)malloc(80 * sizeof(char));
strcpy(str[i],s);
}
}
maxLenStr(str, i, max);
printf("%s\n", str[*max]);
return 0;
}
ZZULIOJ1170: 最长字符串(指针专题)
最新推荐文章于 2022-07-07 11:39:28 发布