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...

  • //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 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.

  • . . 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....

  • 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...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Write a program in C++ that will read a sentence from the keyboard, terminated with a...

    Write a program in C++ that will read a sentence from the keyboard, terminated with a period '.' and write the sentence to an output file with all white spaces replaced by the symbol '*'. You may use the “isspace()” function of the “cctype” library to carry out the determination of whether the character read is 'white space'. Show any #include files you may need. Make sure to handle conditions where the file may be missing so your program does...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

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
ADVERTISEMENT