计算最长的字符串长度

| December 11, 2007 19:25 | timmy | Via Original
编写一个函数int max_len(char *s[ ],  int n),用于计算有n(n<10)个元素的指针数组s中最长的字符串的长度,并编写主程序验证。
例:(括号内为说明)
输入
5
1
333
55555
22
4444
输出
The max_length is:5



#include <stdio.h>
#include <string.h>
int max_len(char *s[],int n);
void main()
{
  int i,n;
  char s[10][80],*p[10];
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%s",s[i]);
    p[i]=s[i];
  }
  printf("The max_length is:%d\n",max_len(p,n));
}
int max_len(char *s[],int n)
{
  int i,res,temp_len;
  res=0;
  for(i=0;i<n;i++)
    res=(strlen(s[i])>res) ? strlen(s[i]):res;
  return res;
}
Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(155)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember