Question

In the language c using the isspace() function: Write a program to count the number of...

In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters.

Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters.

Run your program using the following three sets of input:

            1. You're traveling through

​               another dimension;

              a dimension not only

              of sight and sound,

               but of mind.

            2.Some input which includes several spaces between words and some blank lines

            3. An empty file which mean the user just entering an enter key

As before, part of your grade will be based on the proper use of:

1. meaningful variable names

2. indentation

3. blank lines and spacing

4. comments on the following:

- program description

- function descriptions

- all variable and constant declarations

- ambiguous or complex sections of code

5. the correct use of local variables, local prototypes, and parameter passing

6. format and appearance of output

7. structured code

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

#include <stdio.h>

int main()
{
    // Variable declaration
    int noOfChars = 0; // To count chars
    int noOfWords = 0; // To count words
    int noOfLines = 0; // To count lines
    int ch = ' ';
    int prevCh = ' ';

    // File name
    char* fileName = "data.txt";

    // Get the file
    FILE *file = fopen(fileName, "r");

    // Check if the file exists
    if (!file)
    {
        printf("\nFile %s not found\n", fileName);
    }
    else
    {
        // Read till end of file
        while ((ch = fgetc(file)) != EOF)
        {
            // Check if ch is a space
            if (isspace(ch))
            {
                if ((prevCh != ' ') && ((noOfChars != 0) || (ch == '.'))) // If space is not the first char
                {
                    noOfWords += 1;
                }
            }
            if (ch == '\n')    // Check if ch is a new line
            {
                noOfLines += 1;
            }
            else
            {
                noOfChars += 1;
            }

            // Set prev char
            prevCh = ch;
        }

        // To account for the last line and word
        if ((prevCh != '\n') && (noOfChars != 0))
        {
            noOfLines += 1;
            noOfWords += 1;
        }

        //Close the file
        fclose(file);
    }

    // Display char, word, line count
    printf("\nNo. of characters: %d", noOfChars);
    printf("\nNo. of words: %d", noOfWords);
    printf("\nNo. of lines: %d\n", noOfLines);

    return 0;
}


Add a comment
Know the answer?
Add Answer to:
In the language c using the isspace() function: Write a program to count the number of...
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
  • Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...

    Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words (strings of characters separated by blanks or new lines), and characters in a file (not including blank spaces between words). Write your own version of this program. You will need to create a text file with a number of lines/sentences. The program should accept a filename (of your text file) from the user and then print three numbers: The count of lines/sentences The count...

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • C++ Data Structures TREES You are to write a C++ program to count the frequency (number...

    C++ Data Structures TREES You are to write a C++ program to count the frequency (number of occurrences) of n-grams in a text file. Definition of n-gram is simple: it is the number of consecutive letters in a given text. For example, for the word bilkent the 2-grams (bigrams) are bi, il, lk, ke, en, nt. You may ignore any capitalizations and assume that the text file contains only English letters 'a'...'z', 'A'…'Z', and the blank space to separate words....

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Write a C++ program that will count the number of words and vowels in a sentence....

    Write a C++ program that will count the number of words and vowels in a sentence. Create a bool function called isVowel that accepts a character as a parameter and returns a true if it’s a vowel (aeiou) and false otherwise. Create an int function named countVowels that will accept a string variable as a parameter and will return the number of vowels in it. Call the isVowel function as part of this process. Write an int function named countWords...

  • Write a program that counts the number of characters and words in the following statement: This...

    Write a program that counts the number of characters and words in the following statement: This is a long exercise. I would like to get done with it. END             Hint: In order to count words, count the transitions from non-white space to white space characters Please use C Language and complete program.

  • 1. Write a Python function to count how many python function definitions in a given Python...

    1. Write a Python function to count how many python function definitions in a given Python program. 2. Write a Python function to return the number of word wrapped lines of given number of characters in a given text file. Inside your function you must do it in a single line of code except docstring and comments.

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :)...

    C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :) Use FILE I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each group of adjacent vowels (a, e, i, o, u,...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

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