I recently started to learn C language and I'm currently trying to code a simple program which should return the number of times that a character has been used, but the output is wrong. Why doesn't it work properly? Any help will be really appreciated. Here's the code:
#include <stdio.h>
main() {
int c, i;
int ndigit[25]; //a = 97
while( (c = getchar()) != EOF) {
++ndigit[c - 97];
}
for (i = 0; i < 26; i++) {
putchar(i + 97);
printf(" = %d\n", ndigit[i]);
}
}
And here's the output with the word "hello":
a = 8
b = 0
c = 4203649
d = 0
e = 4202641
f = 0
g = 84
h = 1
i = 12260176
j = 0
k = 1
l = 2
m = -1
n = -1
o = 85
p = 0
q = 1
r = 0
s = 4203673
t = 0
u = 0
v = 0
w = 84
x = 0
y = 0
z = 0