Code_Penguin
Code_Penguin

Reputation: 83

Standard Input - Counting chars/words/lines

I've written some code for finding the # of chars, lines and words in a standard input but I have a few questions.

  1. On running the program - It doesn't grab any inputs from me. Am I able to use shell redirection for this?

  2. My word count - only counts if getchar() is equal to the escape ' or a ' ' space. I want it so that it also counts if its outside of a decimal value range on the ASCII table. IE. if getchar() != in the range of a->z and A->Z or a ', wordcount += 1.

I was thinking about using a decimal value range here to represent the range - ie: getchar() != (65->90 || 97->122 || \' ) -> wordcount+1

https://en.wikipedia.org/wiki/ASCII for ref.

Would this be the best way of going about answering this? and if so, what is the best way to implement the method?

#include <stdio.h>

int main() {
    unsigned long int charcount;
    unsigned long int wordcount;
    unsigned long int linecount;
    int c = getchar();

    while (c != EOF) {
        //characters
        charcount += 1;

        //words separated by characters outside range of a->z, A->Z and ' characters.
        if  (c == '\'' || c == ' ')
            wordcount += 1; 

        //line separated by \n 
        if (c == '\n')
            linecount += 1;
    }    
    printf("%lu %lu %lu\n", charcount, wordcount, linecount);
}

Upvotes: 2

Views: 753

Answers (2)

chqrlie
chqrlie

Reputation: 144959

Your code has multiple problems:

  • You do not initialize the charcount, wordcount nor linecount. Uninitialized local variables with automatic storage must be initialized before used, otherwise you invoke undefined behavior.
  • You only read a single byte from standard input. You should keep reading until you get EOF.
  • Your method for detecting words is incorrect: it is questionable whether ' is a delimiter, but you seem to want to specifically consider it to be. The standard wc utility considers only white space to separate words. Furthermore, multiple separators should only count for 1.

Here is a corrected version with your semantics, namely words are composed of letters, everything else counting as separators:

#include <ctype.h>
#include <stdio.h>

int main(void) {
    unsigned long int charcount = 0;
    unsigned long int wordcount = 0;
    unsigned long int linecount = 0;
    int c, lastc = '\n';
    int inseparator = 1;

    while ((c = getchar()) != EOF) {
        charcount += 1;  // characters
        if (isalpha(c)) {
            wordcount += inseparator;
            inseparator = 0;
        } else {
            inseparator = 1;
            if (c == '\n')
                linecount += 1;
        }
        lastc = c;
    }
    if (lastc != '\n')
        linecount += 1;  // count the last line if not terminated with \n

    printf("%lu %lu %lu\n", charcount, wordcount, linecount);
}

Upvotes: 2

Arif Burhan
Arif Burhan

Reputation: 505

You need:

while((getchar()) != EOF )

As the head of you loop. As you have it getchar will read one character, the while block will loop around with no further getchar() ocurring !

Upvotes: 0

Related Questions