Finding frequency of each letter of a string and adding it to an array
<p>I have written this program, that opens a text file with something written in it, adding each word of it on a string, finding the frequency of each word, giving the ability to search words starting or ending with a certain letter, capitalising the first and last letter of each word and printing them on another text file like a dictionary.</p>

<p>I only want it to do one more thing and it's done but I cannot figure it how! I want it to find the frequency of each letter of the alphabet, sort them from bigger to smaller and then, when the user inputs a letter, it prints the frequency of it!</p>

<p>I tried something as you can see below but it doesn't seem to work!</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#define N 100

int main(){
    FILE *fp1, *fp2;
    char buffer[100];
    char *text;
    char *word;
    char *words[N];
    char temp[N];
    char ch1,ch2,ch3;
    char alphabet[26];
    char temp2, temp3;
    int i=0, y=0, c=0;;
    int word_number=0;
    int n=0;
    int *freq;
    int freq1=0;
    int compare=0;
    int last_letter=0;
    int letter_count[256]={0};
    int temp1=0;

    fp1 = fopen("text.txt", "r");
    fp2 = fopen("output.txt", "w");

    if(fp1==NULL){exit(1);
    }

    while(fgets(buffer,100,fp1)!=NULL){
        if(i==0){
            text=(char*)malloc(strlen(buffer)+1);
            strcpy(text, buffer);
        }
        else{
            text=(char*)realloc(text,n+1+strlen(buffer));
            if(text!=NULL){
                strcat(text,buffer);
            }
            else{
                free(text);
            }
        }
        n=n+1+strlen(buffer);
        i++;
    }
    i=0;

    word=strtok(text," \"\n\t\r,.-;!");
    while(word!=NULL){
        strcpy(words[i],word); i++; word_number++;
        printf("%s\n",word);
        word=strtok(NULL," \"\n\t\r,.-;!");
    }

    for(i=0;i&lt;word_number;i++){   //frequency of words
        y=0;
        while(y&lt;word_number){
            compare=strcmp(words[i],words[y]);
            if(compare==0){ freq1++;
            }
            compare=0;
            y++; 
        }
        freq=(int*)malloc(sizeof(int)*word_number);
        strcpy(freq[i],freq1); freq1=0;
    }

    for(i=0;i&lt;word_number;i++){
        printf("The word: %s ,appears %d times!\n", words[i], freq[i]);
    }

    printf("Search all words starting with the letter: ");
    scanf("%c", &amp;ch1);                                   
    for(i=0;i&lt;word_number;i++){
        if(words[i][0]==ch1){
            printf("%s , ", words[i]);
        }

    }

    printf("Search all words ending with the letter: ");
    scanf("%c", &amp;ch2);
    for(i=0;i&lt;word_number;i++){
        last_letter=strlen(words[i]);
        if(words[i][last_letter]==ch2){
            printf("%s , ", words[i]);
        }
        last_letter=0;
    }

    for(c=0;c&lt;26;c++){  //here starts the part i need help with
        if(words[c]&gt;='a' &amp;&amp; words[c]&lt;='z'){
            letter_count[words[c]-'a']++;
        }
    }

    for(c=0;c&lt;26;c++){
        alphabet[c]=c+'a';
    }

    for(i=0;i&lt;26;i++){     
        for(y=i+1;y&lt;26;y++){
            if(letter_count[i]&gt;letter_count[y]){
                temp1=letter_count[i];
                letter_count[i]=letter_count[y];
                letter_count[y]=temp;

                temp2=alphabet[i];
                alphabet[i]=alphabet[y];
                alphabet[y]=temp2;
            }
        }
    }

    printf("Type a letter to see its frequency: ");
    scanf("%c", &amp;ch3);
    for(c=0;c&lt;26;c++){
        if(alphabet[c]==ch3){
            temp3=c;
        }
    }
    printf("The letter '%c' appears %d times!", alphabet[temp3], letter_count[temp3]); //the part ends here

    for(i=0; i&lt;word_number-1;i++){   //sorting words alphabetically
        for(y=i+1;y&lt;word_number;y++){
            if(strcmp(words[i], words[y])&gt;0){
                strcpy(temp,words[i]);
                strcpy(words[i],words[y]);
                strcpy(words[y],temp);
            }
        }
    }

    for(i=0;i&lt;word_number;i++){        //printing words to second file
        words[i][0]=toUpper(words[i][0]);
        last_letter=strlen(words[i]); 
        words[i][last_letter]=toUpper(words[i][last_letter]);
        last_letter=0;
        fprintf(fp2,"%s\n", words[i]);
    }

    fclose(fp1);
    fclose(fp2);
    return 0;
}
</code></pre>