Thread: How to count total characters from a text file?

  1. #1
    Registered User
    Join Date
    Mar 2013

    How to count total characters from a text file?

    I'm supposed to write a program to read a text file and display the following:
    a) alphabetic letters //finished with
    b) digits // finished with
    c) other characters that are not alphabetic letters and not digits //finished with
    d) total characters read //need help

    The bold part above confused me, by total characters, does it mean the alphabetic letters + other characters? Also, how would I put that in my code?

    #include <stdio.h>
    int main (void)
        int curCh;
        int countCh = 0;
        int digits = 0;
        int countAllOtherCh = 0;
        int countTotalCh = 0;
        FILE* sp1;
        if (!(sp1 = fopen("c:\\bb\\source.txt", "r")))
            printf("Error opening p07-07.DAT for reading");
            return (1);
        while ((curCh = fgetc(sp1)) != EOF)
            if (isalpha(curCh)) 
            else if (isdigit(curCh))
        printf("Number of alphabetic letters is:     %d\n", countCh);
        printf("Number of digits is:                 %d\n", digits);
        printf("Number of all other characters is:   %d\n", countAllOtherCh);
        printf("Number of total characters is:       %d\n", countTotalCh);
        return 0;
    }   // main

  2. #2
    Registered User
    Join Date
    Mar 2013

    Figured it out.

    /close thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-19-2013, 08:37 AM
  2. Replies: 4
    Last Post: 06-16-2012, 12:51 PM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. how do I count the lines in a text file?
    By slow brain in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 02:56 PM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM