#include<stdio.h>
#include<stdlib.h>
#define len 20
int main (void)
{
FILE *fp;
char filename[len];
char character,temp;
int count = 0;
printf("Enter the file name\n");
scanf("%s",filename);
printf("Enter the character to be counted\n");
scanf("%c",&character);
fp=fopen(filename,"r");
while(!feof(fp))
{
temp=fgetc(fp);
if(temp==character)
count++;
}
printf("File '%s' has %d instances of letter '%c'", filename, count, character);
fclose(fp);
return 0;
}
This is my expected output: Write a program to count the number of times a character appears in the File. (Case insensitive... 'a' and 'A' are considered to be the same)
Sample Input and Output:
Enter the file name
test.txt
Enter the character to be counted
r
File 'test.txt' has 99 instances of letter 'r'.
(assuming test.txt in server)**
My output:
*sh-4.2$ gcc -o main *.c
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter the file name
test.txt
Enter the character to be counted
Segmentation fault (core dumped)
sh-4.2$ main
Enter the file name
test.txt
Enter the character to be counted
Segmentation fault (core dumped)*