c - Using histogram to find the most common letter in an array -
this came with, run-time check failure #2 - stack around variable 'h' corrupted.
int mostcommonletter(char s[]) { int i=0, h[26],k=0, max=0, number=0; while ( k < 26){ h[k] = '0'; k++; } while(s[i] != '\0'){ h[whichletter(s[i])] = h[whichletter(s[i])]+1; i++; } h[26] = '\0'; for(i=0;h[i]!='\0';i++){ if(h[i] > max) number=i; } return number; }
you cannot h[26] = '\0'; - h has 26 elements indexed 0..25. know length of h don't need 0-terminate it, for (i=0; < 26; ++i)
also, whichletter returns value in 0..25 range? if e.g. encounters space?
Comments
Post a Comment