I have a program to check if a sentence read on the keyboard is a pangram.
But when displaying the results (in the last for
-loop), it goes out of the expected bounds. If you uncomment the printf
statement, you can see that even thought i<26
is set as the loop condition, it runs upto i = 27
and the array of size 26 has elements cache[26] = garbage value
and cache[27] = 0
. What is wrong with my last for loop ?
int main() {
char* string = (char*)malloc(1024*sizeof(char));
fgets(string, 1024, stdin);
int i=0, cache[25]={0};
while(string[i]!='\0' ){
cache[(toupper(string[i])-'A')]++;
i++;
}
for( i=0; i<26; i++){
// printf("%d - %d\n",i,cache[i]);
if(!cache[i]){
printf("not pangram");
return(0);
}
}
printf("pangram");
return 0;
}