Question

Counting characters, words, and lines based on a text file

I did the assigment but inside main


Write a C program (called counting.c) that counts the number of characters, words and lines read

from standard input (stdin) until EOF is reached. This means counting.c must contain a main

function along with other function.

- Assume the input is ASCII text of any length.

-Every byte read from stdin counts as a character except EOF.

- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has the value 39 –check out the ASCII table) separated by any character outside these ranges.

- Hint: we may wish to create a function that determines whether or not a character is a letter that can be part of a word or not.

- Lines are defined as contiguous sequences of characters separated by newline characters ('\n').

- Characters beyond the final newline character will not be included in the line count.

- On reaching EOF, use this output command:

printf( "%lu %lu %lu\n", charCount, wordCount, lineCount );

where charCount, wordCount and lineCount are all of type unsigned long int. We may need these large types to handle long documents


#include <stdio.h>

int main(void){
    unsigned long int charCount = 0;
    unsigned long int wordCount = 0;
    unsigned long int lineCount = 0;

    int wrd = 1;

    char input;

    while( (input = getchar()) != EOF ){
        //counting characters
        charCount = charCount + 1;
        
        //counts lines
        if (input == '\n'){
            lineCount = lineCount + 1;
            wrd = 1;
        }
    
        //counting words
        //"a"" is 97, "z" is 122, "A" is 65, "Z" is 90, " ' " is 39
        if ((input >= 97 && input <= 122 )||(input >= 65 && input <= 90)||input == 39){
            if (wrd == 1){
                wordCount = wordCount + 1;
                wrd = 0;
            }
        }

        else{
            wrd = 1;
        }
    
    }  

    printf( "%lu %lu %lu\n", charCount, wordCount, lineCount );
    return 0;       

}

But i need to do this in another function which is then called in main, but i dont know how to do that


0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Counting characters, words, and lines based on a text file
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long...

    What are the description of approach or technique used in this? #include <stdio.h> long charCount=0; long digitCount=0; long lineCount=0; long wordCount=0; long digitFreq[10]; int c=0; int outOfWord=0; int state=0; int inWord=1; void printStats(); void totalWords(); void totalLines(); void digitFrequency(); int main(int argc,char **argv) { int i; for(i=0;i<10;i++) { digitFreq[i]=0; } state=outOfWord; c=getchar(); while((c !=EOF)) { charCount++; digitFrequency(c); totalLines(c); totalWords(c); c=getchar(); } printStats(); return 1; } void totalLines(int c) { if(c == '\n') { lineCount++; } } void digitFrequency(int c) {...

  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The...

    Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The Task: The Answer: The completed C code. Below is the file charIO4C.c: #include <stdio.h> #include <ctype.h> #define QUIT_LETTER 'q' int main(void) { int c, line_length; // Each pass through the outer loop reads a line of input. while (1) { printf("\nEnter a line of text. (To quit, start the line with %c.)\n", QUIT_LETTER); c = fgetc(stdin); if (c == EOF || c == QUIT_LETTER)...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • ANSWER ASAP PLEASE Using only the standard input/output function fgetc(), define a C function, called int...

    ANSWER ASAP PLEASE Using only the standard input/output function fgetc(), define a C function, called int readInt(int *value), that reads an integer from the keyboard (stdin), stores it in *value and, returns 1 for success or 0 for fail (in case the user enters nondigit characters). In particular, readInt() reads all characters, one by one, until a blank (or newline) is encountered. The string is then converted to an integer. It is also possible to have the sign charcater (+/-)...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • The program is done in C. This program opens a file containing binary or text and...

    The program is done in C. This program opens a file containing binary or text and reads every byte in the file and writes both the ASCII hex value for that byte as well as it’s printable (human-readable) character (characters, digits, symbols) to standard output. The issue I am having is when the file has multiple lines being read. The first line is read and done properly but the other lines do not work correctly. For instance, the text file...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Edit, compile, and run the following programs on the UNIX shell: Write a program that takes...

    Edit, compile, and run the following programs on the UNIX shell: Write a program that takes in six commandline arguments and has four functions (described below) that use bitwise operators. The user should enter six space-separated commandline arguments: four characters (any ASCII character) followed by two integers. Anything else should print an error message telling the user what the correct input is and end the program. Convert the commandline input into "unsigned char" and "unsigned int" datatypes. Be careful with...

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