// 11-3
// 计算最长的字符串长度
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int max_len(char *s[], int n);
int main(int argc, char *argv[])
{
int n=0;
int len;
char *s[15];
char str[20];
printf("请输入n(n<10)个字符串,以#作为结束标志:\n");
scanf("%s", str);
while (str[0]!='#') // 以'#'作为输入结束的标志
{
s[n] = (char *)malloc(sizeof(char)*(strlen(str)+1));
strcpy(s[n], str);
n++;
if (n>=9) // 最多输入9个字符串
{
break;
}
scanf("%s", str);
}
len = max_len(s, n);
printf("指针数组中最长的字符串的长度为: %d\n", len);
return 0;
}
int max_len(char *s[], int n)
{
int i;
int len=0;
int temp;
for (i=0; i<n; i++)
{
temp = strlen(s[i]);
if (temp>len)
{
len = temp;
}
}
return len;
}