How to count total characters from a text file?

This is a discussion on How to count total characters from a text file? within the C Programming forums, part of the General Programming Boards category; I'm supposed to write a program to read a text file and display the following: a) alphabetic letters //finished with ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    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?


    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)) 
                countCh++;
            else if (isdigit(curCh))
                digits++;
            else 
                countAllOtherCh++;        
           }
           
                 
        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);
           
        
        printf("\n\n");      
        return 0;
    }   // main

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    NVM,

    Figured it out.

    /close thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-19-2013, 09:37 AM
  2. Replies: 4
    Last Post: 06-16-2012, 01:51 PM
  3. Replies: 1
    Last Post: 09-10-2005, 07: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, 02:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21