【問題描述】
找出最長的字符串。輸入5個字符串,輸出其中最長的字符串。輸入字符串調用函數scanf("%s",sx)。如果最長的字符串有多個,則打印第一個。請自行設計int StrLength(char *)函數,求解字符串長度,不允許調用系統函數。
【輸入形式】
首先打印提示"Input 5 srings:";然后直接在冒號后面輸入五個字符串,每個字符串之間用空格或回車或制表符隔開。
【輸出形式】
首先打印"The longest is:";緊跟后面輸出最長的一個字符串;換行。
【運行時的輸入輸出樣例】
Input 5 srings:li
wang
zhang
jin
xian
The longest is:zhang
#include <iostream>
#include <stdio.h>
using namespace std;
int StrLength(char *);//求解字符串長度
int main()
{
char name[5][100];
int len[5];
cout << "Input 5 strings:";
for(int i=0;i<5;++i)
{
scanf("%s",name[i]);
len[i] = StrLength(name[i]);
}
int Max = len[0];
int index = 0;
for(int i=1;i<5;++i)
if(Max<len[i])
{
Max = len[i];
index = i;
}
cout << "The longest is:" << name[index] << endl;
return 0;
}
int StrLength(char * name)
{
int len = 0;
for(int i=0;name[i]!='\0';++i,++len);
return len;
}