【发布时间】:2017-09-10 14:58:03
【问题描述】:
我开始学习 c。我正在尝试编写一个代码来计算句子中 a 和 A 的数量,但是代码每次执行时都会给我随机数,即使它是同一个句子。
#include <stdio.h>
#include <string.h>
int main()
{
char words[10000000000];
int i;
int acount = 0;
int Acount = 0;
char A[0]="A";
char a[0]="a";
printf("Input your sentence to be counted.");
scanf("%s",words);
printf("%s",words);
for (i=0;i<=sizeof(words);i++)
{
if (words[i]==a[0]){
acount++;
}else if (words[i]==A[0]){
Acount++;
}
}
printf ("\nThe number of A's is %i. The number of a's is %i.",Acount,acount);
return 0;
}
【问题讨论】:
-
只是提醒一下 - 该数组将尝试分配 10GB 的内存(对于这种情况可能有点过分)。在其他新闻中,请提供mcve,让我们更好地帮助您。
-
char A[0]="A";是一个错误。那甚至不应该编译。 -
你的操作系统是什么?因为我不知道默认情况下允许这么多堆栈的任何操作系统。
-
@melpomene 编译时会出现警告。
-
您可能想阅读以下内容:How to debug small programs