题目:输入N个字符串,输出字符出现最多的字符串。 总是提示内存错误怎么回事

#include "stdafx.h"
#define m 5
#include "string.h"
void main(int argc, char* argv[])
{char s[m][40],max[1][40];
int i,n;
printf("input the n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",&s[i]);

max[0][40]=s[0][40];

for(i=0;i<n;i++)
{if(strlen(s[i])>=strlen(max[0]))
max[0][40]=s[i][40];
}
printf("%s\n",max[1][40]);
}

唉,错误太多了 基本的语法都有错误
1、scanf("%s",&s[i]);
你的字符串数组是个二维数组 其实 s[i] 这个已经是二维数组第 i 行的字符串的首地址了,不用加&符号了 scanf("%s",s[i]);

2、max[0][40]=s[0][40];
C语言的字符串拷贝是有单独的函数的,不支持直接等号赋值 strcpy(max[0],s[0]);

3、
if(strlen(s[i])>=strlen(max[0]))
max[0][40]=s[i][40];
这里也是 使用库函数赋值 strcpy(max[0],s[i]);

4、printf("%s\n",max[1][40]);
这里的 前面定义的数组是只有一行,娶不到 1 的,最后输出应该是
printf("%s\n",max[0]);

5、max[1][40]) 你字符串数组想的定义,从程序功能上看没必要 定义二维数组,定义一个一维的字符串数组即可 例 max[40];
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-16
//实际上还可以更简答
//但是我还是按你思路给你改好了
input the n
3
ewrwgdfgfh
32423
xcv
ewrwgdfgfh
Press any key to continue

#include "stdafx.h"
#define m 5
#include "string.h"
void main(int argc, char* argv[])
{
char s[m][40],max[1][40];
int i,n;
printf("input the n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",&s[i]);

strcpy(max[0],s[0]);

for(i=0;i<n;i++)
{
if(strlen(s[i])>=strlen(max[0]))
strcpy(max[0],s[i]);
}
printf("%s\n",max[0]);
}
第2个回答  2011-12-16
max[0][40]=s[0][40];
感觉你的意思是吧s赋值给max,字符串不能这么操作,因为[40]已经越界了
可以用字符串拷贝命令
strcmp(max[0],s[0]);

scanf("%s",&s[i]);
这一句,变成 gets(s[i]);

max[1][40];
这个没必要这样定义,定义为max[40];就可以
第3个回答  2011-12-16
#include "stdio.h"
#define m 5 //这里要注意输入的字符串数不能超过5
#include "string.h"
void main(int argc, char* argv[])
{
char s[m][40],max[1][40];
int i,n;
printf("input the n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",&s[i]);
}

strcpy(max[0],s[0]); //字符数组是不能直接赋值的

for(i=0;i<n;i++)
{
if(strlen(s[i])>=strlen(max[0]))
strcpy(max[0],s[i]); //同上
}
printf("%s\n",max[0]); //max[1]是越界的
}
第4个回答  2011-12-16
设置断点,看看到哪里出错,粗看了一下,最后一句printf("%s\n",max[1][40]);是不是应该是printf("%s\n",max[0][40]);
第5个回答  2011-12-16
这太难了,不会

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网