First make the corrections suggested in @artm's answer.
But regarding your sorting question. The C standard library has one (or more, depending on the version) routines for sorting an array of any type. The most common is qsort(3) . The rubbing is that you have to give the subroutine a function that it can use to compare the elements of the array. Thus, you need a way to store both the character and its appearance together, and you need to write this comparison function.
I would suggest taking the @jack comment and using an array of structures. Each structure will look something like this:
struct letter { char character; int count; };
to group both the character and its number of occurrences. Then select an array of them, one for each letter (and not just int[] , as you have now).
struct letter letters[26]; for (int i = 0; i < 26; i++) { letters[i] = (struct letter){ (char)(i + 'a'), 0 }; }
In your loop looking at the line received from the user, update the letter.count field for the corresponding letter each time it is visible.
Then you can use the standard qsort(3) library to sort the array of letters according to the counts inside them. To use this function, you need a comparison function that tells the library the ordering of two elements (more or less).
The comparison function must have a signature: int (*compar)(const void *, const void *) . The void* element points to the individual elements in your letters array, so you need to assign them to a struct letter and compare them in them. It might look something like this (untested):
int letter_cmp(const void* first, const void* second) { struct letter* first_letter = (struct letter *) first; struct letter* second_letter = (struct letter*) second; if (first_letter->count == second_letter->count) { return 0; } else if (first_letter->count < second_letter->count) { return -1; } return 1; }
Then you call the qort(3) function as follows.
qsort(&letters[0], sizeof(letters), sizeof(struct letter), &letter_cmp);
The first argument is the beginning of the array. The second is the size of the array in elements. The third size of each element, and the fourth is a comparison function.
After this call, the letters array was sorted in ascending order of count . So letters[0].character gives you the character with the smallest count , and letters[25].character gives you the one with the highest (most cases).