Programming Fundamentals:File Handling (Session 7)


Character count

#include<stdio.h>
#include<stdlib.h>
int main() 
{
  char file1[100];
  int count=0;
  char c,ch,ck;
  FILE *fptr;printf("Enter the file name\n");
  scanf("%s",file1);
  fptr=fopen(file1,"r");
  printf("Enter the character to be counted\n");
  scanf(" %c",&c);
  ck=c;
  if((int)c>=65 &&(int)c<=90)
    c=(int)c+32;
  while((ch=getc(fptr)))
  {
    if((int)ch>=65 && (int)ch<=90)
      ch=(int)ch+32;
    if(ch==EOF)
      break;
    else if(ch==c)
      count+=1;
  }
  printf("File '%s' has %d instances of letter '%c'.",file1,count,ck);
  fclose(fptr);
  return 0;
}

File copy

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;
 
   printf("Enter the input file name\n");
   scanf("%s",source_file);
 
   source = fopen(source_file, "r");
   printf("Enter the output file name\n");
   scanf("%s",target_file);
   target = fopen(target_file, "w");
   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, target);
   fclose(source);
   fclose(target);
 
   return 0;
}
SHARE
    Blogger Comment
    Facebook Comment

7 comments :

  1. character count is not working

    ReplyDelete
  2. Thanks For sharing this Superb article. I use this Article to show my assignment in college. it is useful For me Great Work.

    java forums

    ReplyDelete
  3. Where can I get programs for sessions 8 to 12 which includes linear&binary search,sorting,stack&queue and arrays?

    ReplyDelete
  4. character count is not working.please post correct program...

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Word count is an usefull tool to count Word, Line, Page and Character in multiple files and also you can calculate amount and generate reports. word count tool

    ReplyDelete
  7. Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! read

    ReplyDelete

loading...