D.Kenny
D.Kenny

Reputation: 121

How to count the number of words, characters and lines with pure C

I want to count the number of words, characters, new lines, no matter how my sentence looks like. (For example, even if I typed a sentence like this:

y yafa \n \n youasf\n sd

the program should still be able to count the number of words, lines, characters correctly). I don't know how to implement such program with pure C, can anyone help me on that?

Here is my current code, and it can only be correct under certain conditions...

int main() {
    int cCount = 0, wCount = 0, lCount = 0;

    printf("Please enter the sentence you want\n");

    char str[20];
    int j7 = 0;

    while (int(str[j7]) != 4) {
        str[j7] = getchar();
        if (str[0] == ' ') {
            printf("Sentence starts from a word not blank\n");
            break;
        }
        if (int(str[j7]) == 4)
            break;
        j7++;
    }
    int count = 0;
    while (count < 20) {
        if (str[count] != ' ' && str[count] != '\n') {
            cCount++;
        } else
        if (str[count] == ' ') {
            wCount++;
        } else
        if (str[count] == '\n') {
            lCount++;
        }
        count++;
    }

    printf("Characters: %d\nWords: %d\nLines: %d\n\n",
           cCount, wCount++, lCount + 1);
    int x = 0;
    std::cin >> x;
}

Upvotes: 1

Views: 189

Answers (1)

chqrlie
chqrlie

Reputation: 145287

You are not writing in Pure C, but rather in C++.

To achieve your goal, you must summarize the problem into a sequence of logical steps:

  • for each character read:
    • if the previous one is a line separator, you have a new line ;
    • if the previous one is a word separator and the current isn't, you have a new word ;
    • if all cases, you have a new character, save it as the previous character for the next iteration.

Use '\n' as the initial value for the last character, so the first character read if any starts a new line and possibly a new word if it is not whitespace.

Here is a simple implementation:

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

int main(void) {
    long chars = 0, words = 0, lines = 0;

    printf("Enter the text:\n");

    for (int c, last = '\n'; (c = getchar()) != EOF; last = c) {
        chars++;
        if (last == '\n')
            lines++;
        if (isspace(last) && !isspace(c))
            words++;
    }
    printf("Characters: %ld\n"
           "Words: %ld\n"
           "Lines: %ld\n\n", chars, words, lines);
    return 0;
}

If you are required to use a while loop, the for loop can be converted this way:

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

int main(void) {
    long chars = 0, words = 0, lines = 0;
    int c, last = '\n';

    printf("Enter the text:\n");

    while ((c = getchar()) != EOF) {
        chars++;
        if (last == '\n')
            lines++;
        if (isspace(last) && !isspace(c))
            words++;
        last = c;
    }
    printf("Characters: %ld\n"
           "Words: %ld\n"
           "Lines: %ld\n\n", chars, words, lines);
    return 0;
}

Upvotes: 2

Related Questions