求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母。
#include<stdio.h>
#include<string.h>
int main()
{
char a[4][401],str[101];
int i, max;
printf("请输入字符串:");
scanf_s("%s", str);
max = strlen(str);
for (i = 0; i < 4; i++)
{
scanf_s("%s", str);
if (max < strlen(a[i]))
{
max = strlen(a[i]);
strcpy(str, a[i]);
}
}
printf("%s", str);
return 0;
}
意思是strcpy不安全,让你改为strcpy_s函数
strcpy(str, a[i]);改为
strcpy_s(str, 100,a[i]);
修改处见注释,供参考:
#define _CRT_SECURE_NO_WARNINGS //修改
#include <stdio.h>
#include <string.h>
int main()
{
char a[4][101], str[101]; //char a[4][401],
int i, max;
printf("请输入字符串:");
scanf_s("%s", str, 101); //scanf_s("%s", str);
max = strlen(str);
for (i = 0; i < 4; i++)
{
scanf_s("%s", a[i], 101); //scanf_s("%s", str);
if (max < strlen(a[i]))
{
max = strlen(a[i]);
strcpy(str, a[i]);
}
}
printf("%s", str);
return 0;
}