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)
Input and Output Format:
Refer sample input and output for formatting specifications. All text in bold corresponds to input and the rest corresponds to output.
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'.
Code:
#include<stdio.h> #include<ctype.h> #include<stdlib.h> int main(void) { FILE *fptr=NULL; char c[2],get; int ch; int count=0; char filename[100]; printf("Enter the file name\n"); scanf("%19s",filename); fptr=fopen(filename,"r"); printf("Enter the character to be counted\n"); scanf(" %s",c); if(fptr==NULL) { exit(-1); } if(((int)c[0]>=65)&&((int)c[0]<=122)) { if(((int)c[0]>=97)&&((int)c[0]<=122)) ch=(int)c[0]-32; else ch=(int)c[0]+32; } do{ get=fgetc(fptr); if(((int)get==ch)||get==c[0]) count=count+1; }while(get!=EOF); printf("File '%s' has %d instances of letter '%s'.",filename,count,c); fclose(fptr); return 0; }
its compiling but not running completely
ReplyDeleteThanks it worked :)
ReplyDeletecan you plzz help me..the same program compiled but i am not getting output..
Deletecan you provide me correct code for this program
Deletehelo pls tell how it worked...i didnt get that last printf in the scree
Deletehelo pls tell how it worked...i didnt get that last printf in the scree
Deletethank u so much...its working
ReplyDeletehello vinothini..I am not getting output for this program ,,can you plzz help me to provide correct code for this program..
Deletebro did u get the output for this prgrm???
Deletecan anyone help its not working
ReplyDeletethe exact output the last line the no of instances counted is not coming..... plsss help
ReplyDeleteits not working, that test,txt file is showing null
ReplyDeleteHello everyone,if you are not getting answer for above code then it might be one reason that you have not uploaded test.txt file from MYFILES which is at the bottom of your code .So for that create text file named test.txt and write some characters and upload it.
ReplyDeleteHow to do that??
DeleteCompilation Errors -
Delete1496248071504_k.chaithanyareddy@yahoo.com/Main.c:20:20: error: lvalue required as left operand of assignm
This comment has been removed by the author.
ReplyDeletePls don't use FILE*fptr=NULL; instead use FILE*fptr;
ReplyDeleteAlso omit following statements from d code:
if(fptr==NULL)
{
exit(-1);
}
Doing above things removed my compilation errors and program executed correctly
Thank you bro.. u r right.
DeleteGuys, do these things it ll work.
even after doing those correction i can't get the last output statement . hai mr. Ramesh Thangadurai could u pls post ur complete program.
ReplyDeletecan i get the logic in java
ReplyDeleteis this coding in java or python
ReplyDelete