Wednesday, 16 May 2012

Write a C program that will write input into a file, then it will return frequency of each letter occurred in the file.

Underlying Concept: File Handling

Note: Press CTRL-D for taking EOF as input

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
        FILE *f1,*f2;
        char ch,bh;
        printf("\n write to file INPUT.txt:\n");
        f1=fopen("FREQUENCY.txt","w");
        while((ch=getchar())!=EOF)
                putc(ch,f1);
        fclose(f1);
    int n=0;
    f2=fopen("histogram.txt","w");
    unsigned char r;
    printf("\n\n");
   
    for(r=97;r<122;r++)
    {
        n=0;
        if(r%5==0)
            printf("\n");
        f1=fopen("FREQUENCY.txt","r");
        while((ch=getc(f1))!=EOF)
        {
            if(ch==r)
                n++;
        }
   
            printf(" Frequency of %c is %d\t",r,n);
            fprintf(f2,"%d",n);
            fprintf(f2,"\n");
        fclose(f1);
    }
    fclose(f2);
    return 0;
}

0 comments

 
© 2011-2012 ProgrammingBlue
Posts RSS Comments RSS
Back to top