Counting characters, words, lines an paragraphs in C


Keywords:c 


Question: 

I'm trying to count the characters, words, lines an paragraphs in C from the stdin.

something is not working and I don't know why.

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int pCount=0, parCount=0, cCount=0, lCount=0;
    double prom=0;
    char c;
    int newln_cnt=0;
    while ((c=getchar())!=EOF){ 
        cCount++;
        switch (c)
        {
            case '\n':
                newln_cnt++;
                lCount++;
                if (newln_cnt == 2)
                {
                    parCount++;
                    newln_cnt = 0;
                }
                break;
            case ' ':
                pCount++;
                break;
        }               
    }
    prom = (cCount / pCount);
    printf("Total caracteres: %d \n", cCount);
    printf("Cantidad palabras: %d \n", pCount);
    printf("Cantidad líneas: %d \n", lCount);
    printf("Cantidad párrafos: %d \n", parCount);
    printf("Promedio longitud palabra: %f \n", prom);
    return 0;
}

it kinda works with the characters (it shows one less). but the rest is all bad.

Input:

Oid, mortales, el grito sagrado:
"Libertad, libertad, libertad!"

Oid el ruido de rotas cadenas,
ved en trono a la noble igualdad.

Ya su trono dignisimo abrieron
las Provincias Unidas del Sud

y los libres del mundo responden:
"Al gran pueblo argentino, salud!
Al gran pueblo argentino, salud!"

Y los libres del mundo responden:
"Al gran pueblo argentino, salud!"

Sean eternos los laureles
que supimos conseguir,
que supimos conseguir.

Coronados de gloria vivamos...
o juremos con gloria morir!,
o juremos con gloria morir!,

o juremos con gloria morir!

Expected Ouput:

Total caracteres: 558
Cantidad palabras: 87
Cantidad líneas: 25
Cantidad párrafos: 8
Promedio longitud palabra: 4.966

my ouput:

Total caracteres: 557
Cantidad palabras: 69
Cantidad líneas: 24
Cantidad párrafos: 12
Promedio longitud palabra: 8.000

The program counts the number of characters, words, lines and paragraphs (two consecutive '\n'). and the averge word length .


3 Answers: 

Each your count conditions are wrong.
fix like follows:

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

int main(void){
    int pCount=0, parCount=0, cCount=0, lCount=0;//word, paragraph, character, line
    int abCount = 0;//alphabet 
    double prom=0;
    int c;//It should be int.
    char pprev = '\n', prev = '\n';

    while ((c=getchar())!=EOF){
        ++cCount;
        if(isalpha(c))
            ++abCount;
        if(isspace(c)){
            if(c == '\n'){
                ++lCount;
            }
        } else if(isspace(prev)){//isspace(prev) && !isspace(c) : edge of top of word
            ++pCount;
            if(pprev == '\n' && prev == '\n'){//edge of top of paragraph
                ++parCount;
            }
        }
        pprev = prev;
        prev = c;
    }
    if(prev != '\n'){//If the file is not terminated by newline
        ++lCount;
    }

    prom = (double)abCount / pCount;//(cCount - spcCount - punctCount) / pCount
    printf("Total caracteres: %d \n", cCount);
    printf("Cantidad palabras: %d \n", pCount);
    printf("Cantidad lineas: %d \n", lCount);
    printf("Cantidad parrafos: %d \n", parCount);
    printf("Promedio longitud palabra: %.3f \n", prom);
    return 0;
}


I see several problem in your code:

  1. Paragraphs count: you do not set newln_cnt to 0 if read character is different of \n. This will count one paragraph each time two \n are read.

  2. Spaces count: you only consider ' ' characters, you may miss other white space characters like \t ou non breakable space. consider using isspace() function.

  3. Mean line length: you divide two integers to get a float, consider casting:

    prom = (float)cCount / (flao)pCount;
    

My advice: Start with a short text (3 words per line, 5 lines) and a debugger.



It didn't compile because of type conversion errors, but you can use floats for everything and it will compile:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    double pCount=0, parCount=0, cCount=0, lCount=0;
    double prom=0;
    char c;
    int newln_cnt=0;
    while ((c=getchar())!=EOF){ 
        switch (c)
        {
            case '\n':
                newln_cnt++;
                lCount++;
                if (newln_cnt == 2)
                {
                    parCount++;
                    newln_cnt = 0;
                }
                break;
            case ' ':
                pCount++;
                break;
        }               
    }
    prom = (cCount / pCount);
    printf("Total caracteres: %f \n", cCount);
    printf("Cantidad palabras: %f \n", pCount);
    printf("Cantidad líneas: %f \n", lCount);
    printf("Cantidad párrafos: %f \n", parCount);
    printf("Promedio longitud palabra: %f \n", prom);
    return 0;
}

Now that the program compiles, you can adjust to whatever types are best for you program, you might even have a type of your own.

A famous program that works like your program is wc - word count and is part of the standard Unix libraries.