user1593866
user1593866

Reputation: 17

how to input a file and use it to count the upper/lower-case letters, numbers, and punctuation?

My goal is simple. To ask a user for a simple text file containing upper/lower case letters, digits and punctuation. Then take the file, open it, and scan for each of the aforementioned elements keeping a count of each. Lastly, output a total for each (i.e. total uppercase letters contained, total lowercase letters, total digits and total punctuation marks).

Unfortunately, my question is not so simple. I am confused really at how exactly (i.e. at the concept of, and syntax for) the way C passes an input file to a function like fgetc and then tests it to increment counters. My program crashes and I an running low on ideas why. I guess my initial question then is, why does it crash? My code is below;

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main()
{
    char Character = 0;
    int words = 0;
    int upper_case = 0;
    int lower_case = 0;
    int punctuation = 0;
    int digits = 0;
    int entered_words = 0;
    char user_filename[100];
    char user_filecontent[100];



    printf("Enter the filename of the file containing text and/or digits:\n");
    printf("(hit the Enter key when done)\n");
    gets(user_filename);

    FILE *fp;
    fp = fopen (user_filename, "r");


    if (fp == NULL)
    {
        printf("\nError, Unable to open the file for reading\n");
    }


    while((fp = fgetc(fp)) != EOF)
    {


            while((Character=fgetc(fp)) != '\n');
            {

                if (isalnum (Character))
                {
                    if (!entered_words)
                    {
                        entered_words = 1;
                        words++;
                    }
                }


                else
                {
                    if (entered_words)
                    {
                        entered_words = 0;
                    }
                }


                if (isupper(Character))
                {
                    upper_case++;
                }


                else if (islower(Character))
                {
                    lower_case++;
                }


                else if (isdigit(Character))
                {
                    digits++;
                }


                else if (ispunct(Character))
                {
                    punctuation++;
                }

            }

    fclose(fp);
    }

    printf("Total number of words is %d.\n", words);
    printf("Total number of digits are %d.\n", digits);
    printf("Total number of uppercase letters is %d.\n", upper_case);
    printf("Total number of lowercase letters is %d.\n", lower_case);
    printf("Total number of punctuation characters is %d.\n", punctuation);

return 0;
}

Upvotes: 0

Views: 7090

Answers (3)

drjd
drjd

Reputation: 399

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main()
{
    char Character = 0;
    int words = 0;
    int upper_case = 0;
    int lower_case = 0;
    int punctuation = 0;
    int digits = 0;
    int entered_words = 0;
    char user_filename[100];
    char user_filecontent[100];



    printf("Enter the filename of the file containing text and/or digits:\n");
    printf("(hit the Enter key when done)\n");
    gets(user_filename);

    FILE *fp;
    fp = fopen (user_filename, "r");


    if (fp == NULL)
    {
        printf("\nError, Unable to open the file for reading\n");
    }


    while((Character = fgetc(fp)) != EOF)
    {

                if (isalnum (Character))
                {
                    if (!entered_words)
                    {
                        entered_words = 1;
                        words++;
                    }
                }


                else
                {
                    if (entered_words)
                    {
                        entered_words = 0;
                    }
                }


                if (isupper(Character))
                {
                    upper_case++;
                }


                else if (islower(Character))
                {
                    lower_case++;
                }


                else if (isdigit(Character))
                {
                    digits++;
                }


                else if (ispunct(Character))
                {
                    punctuation++;
                }
    }

    fclose(fp);

    printf("Total number of words is %d.\n", words);
    printf("Total number of digits are %d.\n", digits);
    printf("Total number of uppercase letters is %d.\n", upper_case);
    printf("Total number of lowercase letters is %d.\n", lower_case);
    printf("Total number of punctuation characters is %d.\n", punctuation);

return 0;
}

I removed the inner while loop and moved the fclose function call outside the loop.

Upvotes: 1

Manuel Selva
Manuel Selva

Reputation: 19060

man fgetc tells

fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

You must use an int variable to store this result

Upvotes: 0

pb2q
pb2q

Reputation: 59637

You're assigning to your FILE *fp the result of an fgetc in your while test:

while((fp = fgetc(fp)) != EOF)

This is probably the cause of your crash. fp is the handle to your file once you've called fopen, after that you only need to pass it to functions that take a FILE *, like fgetc and fclose.

Also you're calling fgetc twice: once in the while test, and then immediately following in another while test. You'll lose characters this way, even after you fix the fp problem. Why do you need the inner loop?

Upvotes: 0

Related Questions