无法输出,告诉我错哪了,和怎么改
#include <stdio.h>
#include <string.h>
int main()
{
int n = 5, i, j, t;
char b[n], a, *k;
for (i = 0; i < n; i++)
{
scanf("%c", &b[i]);
}
k = b;
t = strlen(b);
a = *k;
for (i = 1; i < n; i++)
{
if (strlen(b[i]) > t)
{
t = strlen(b[i]);
a = *(k + i);
}
}
printf("%c", a);
return 0;
}
if(strlen(&b[i])>t){t=strlen(&b[i]); a=*(k+i);}
还是不正常
要求找出最长的字符串。输入5个字符串,输出其中最长的字符串。输入字符串调用函数scanf(“%s”,str)。(若最长的字符串不只一个,则输出最先输入的字符串)
输入输出示例:
Input 5 strings: red blue yellow green purple
The longest is: yellow
#include <stdio.h>
#include <string.h>
int main()
{
int n = 5, i, j, t;
char b[5][1024], *a, * k;
printf("Input 5 strings: ");
for (i = 0; i < n; i++)
{
scanf("%s", &b[i]);
}
//k = b;
t = strlen(b[0]);
a = b[0];
for (i = 1; i < n; i++)
{
if (strlen(b[i]) > t)
{
t = strlen(b[i]);
a = b[i];
}
}
printf("The longest is %s", a);
return 0;
}
t = strlen(b);是错的
#include <stdio.h>
#include <string.h>
int main()
{
int n = 5, i, t, a;
char b[n][100];
printf("Input 5 strings:");
for (i = 0; i < n; i++)
{
scanf("%s", &b[i]);
}
a = 0;
t = strlen(b[0]);
for (i = 1; i < n; i++)
{
if (strlen(b[i]) > t)
{
t = strlen(b[i]);
a = i;
}
}
printf("The longest is:%s", b[a]);
return 0;
}
供参考:
#include <stdio.h>
#include <string.h>
int main()
{
int n = 5, i, maxlen=0;
char str[256]={0}, s[256]={0};
printf("Input %d strings:",n);
for (i = 0; i < n; i++)
{
scanf("%s", str);
if(i==0){
maxlen=strlen(str);
strcpy(s,str);
}
else if(maxlen < strlen(str)){
maxlen=strlen(str);
strcpy(s,str);
}
}
printf("The longest is:%s",s);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
int n = 5, i, j, t;
char b[n], a, *k;
for (i = 0; i < n; i++)
{
scanf("%c", &b[i]);
}
k = b;
t = strlen(b);
a = *k;
for (i = 1; i < n; i++)
{
if (strlen(&b[i]) > t)
{
t = strlen(&b[i]);
a = *(k + i);
}
}
printf("%c", a);
return 0;
}