Monday 14 March 2016

File : Count Character


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;
}

21 comments:

  1. its compiling but not running completely

    ReplyDelete
  2. Replies
    1. can you plzz help me..the same program compiled but i am not getting output..

      Delete
    2. can you provide me correct code for this program

      Delete
    3. helo pls tell how it worked...i didnt get that last printf in the scree

      Delete
    4. helo pls tell how it worked...i didnt get that last printf in the scree

      Delete
  3. thank u so much...its working

    ReplyDelete
    Replies
    1. hello vinothini..I am not getting output for this program ,,can you plzz help me to provide correct code for this program..

      Delete
    2. bro did u get the output for this prgrm???

      Delete
  4. can anyone help its not working

    ReplyDelete
  5. the exact output the last line the no of instances counted is not coming..... plsss help

    ReplyDelete
  6. its not working, that test,txt file is showing null

    ReplyDelete
  7. Hello 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.

    ReplyDelete
    Replies
    1. Compilation Errors -
      1496248071504_k.chaithanyareddy@yahoo.com/Main.c:20:20: error: lvalue required as left operand of assignm

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Pls don't use FILE*fptr=NULL; instead use FILE*fptr;
    Also omit following statements from d code:
    if(fptr==NULL)
    {
    exit(-1);
    }
    Doing above things removed my compilation errors and program executed correctly

    ReplyDelete
    Replies
    1. Thank you bro.. u r right.
      Guys, do these things it ll work.

      Delete
  10. even after doing those correction i can't get the last output statement . hai mr. Ramesh Thangadurai could u pls post ur complete program.

    ReplyDelete
  11. can i get the logic in java

    ReplyDelete
  12. is this coding in java or python

    ReplyDelete