Program to count number of occurrences of given character in the file using command line arguments.


C File =>



/* Write a C program to accept file name and character as command line arguments and count number of occurrences of given character in the file. */


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char *argv[])
{
    char ch,cnt=0;
    char c=argv[2];
    FILE *fp;
    fp=fopen(argv[1],"r");

    while((ch=fgetc(fp))!=EOF)
    {
         if(ch==c)
           cnt++;
     }
 printf("\n Count of %c = %d",argv[2],cnt);
return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.