题目:读取文件中的字符串,统计从“a”到“z”26 个字母各自出现的次数,并将
结果放入数组中。如文件中有字符串 abcdefgabcdeabc,输出 33322110000000000000000000。
我写的代码
#include <stdio.h>
int main()
{
int a[123]={0};//z的ascii码122方便
char *s;
scanf("%s",s);
char *p;//定义另一个指针来搜索字母
for(p=s;*p!='\0';p++)//将大写字母也算进去
if(*p>='A'&&*p<='Z')
*p+=32;
for(p=s;*p!='\0';p++)
if(*p>='a'&&*p<='z')
a[(int)(*p)]++;
for(int i=97;i<123;i++)
printf("%d",a[i]);
}
用devc++编译没问题。但是在网上在线编译有问题
报错3 Segmentation fault (core dumped) LD_LIBRARY_PATH=/usr/local/gcc-9.2.0/lib64 ./a.out
Exited with error status 139
去网上查了一下好像是指针问题。
谢谢回答。