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

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

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

  • Consider the following snippet of code. (Assume that input strings are at most 255 characters long.)...

    Consider the following snippet of code. (Assume that input strings are at most 255 characters long.) char* to_upper_case(char* original) { char capstr[255]; unsigned int i; for (i = 0; original[i] != '\0'; ++i) { if (original[i] >= 'a' && original[i] <= 'z') { capstr[i] = original[i] - (char)'a' + (char)'A'; } else { capstr[i] = original[i]; } } capstr[i] = '\0'; return capstr; } void main() { printf("%s", to_upper_case("the c programming language")); } a) What will be the output? Trace...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

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