Question

Write a program that will tell the user how many characters and how many lines a...

Write a program that will tell the user how many characters and how many lines a file contains. You may assume that each line is no more than 1000 characters long.

To do this program:

  1. Get the name of the file from the command line. If no filename was given, display an error message and exit.
  2. Open the file for reading.
  3. Loop through the file, one line at a time.
  4. Count the lines as you go. Also keep track of the total number of characters. Use the strlen function to tell you how long a line is. The total count should include newlines and spaces.
  5. When done, close the file and output the two values, like this:
    lines: 23
    characters: 832

Also display the number of words in the file.

For our purposes, a word is a sequence of alphanumeric characters separated by one or more spaces. A space is any whitespace character: space, tab ('\t'), or newline ('\n'). The ctype.h header file contains a function called isalnum(c) that will tell you if the given character is alphanumeric. And there is a function called isspace(c) that will tell you if the given character is a whitespace.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CountWord.c:

#include <stdio.h>
#include <string.h>

int main ( void )
{
    char file_name[100];
    int lineCount, j,numWord=0,numChar=0,totalChar=0,totalWords=0;
    char array[1000];
    char line[1000];
    char ch;
    printf("Enter the file name: ");
    scanf("%s", file_name);
    FILE *file = fopen ( file_name, "r" );      
    if ( file != NULL )
    {
        lineCount=0;
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
            strcpy(array, line);
            for(j=0; j<strlen(array); j++)
            {
                ch = array[j];
                if(isalnum(ch)||isspace(ch))
                    numChar++;
                if (isspace(ch))
                    numWord++;
            }
            lineCount++;
        }
        printf("Number of Lines: %d\n",lineCount);
        printf("Number of Words: %d\n",numWord);
        printf("Number of Characters: %d\n",numChar);
        fclose ( file );
    }
    else
    {
        perror ( file_name ); /* why didn't the file open? */
    }
    return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
Write a program that will tell the user how many characters and how many lines a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT