Home  >  Article  >  Operation and Maintenance  >  Use c language to write wc command - count the number of characters, words and lines

Use c language to write wc command - count the number of characters, words and lines

2020-10-26 11:26:364637browse

We know that there is a very commonly used command on the Linux operating system, the wc command used to count the number of characters, words and lines. Today, let’s try to use C language to write a program with similar functions (Note: Reading this article requires a certain basic knowledge of C language).

When writing this program, you need to master the usage of two functions, getchar() and putchar().

Getchar is used to read a character from the standard input, while putchar is used to print a character to the standard output. Counting the number of standard input characters is relatively simple. As long as the getchar function can still read characters, the variable counting the number of characters will increase by 1. Counting the number of lines is also simple. As long as the character read is a newline character\n, the variable of the statistical function will be increased by 1.

The main difficulty here is how to count the number of words. My idea here is to set a state variable IN_WORD. When the characters read are blank characters (spaces, horizontal tabs, and newlines are all is a blank character), the IN_WORD value is 0, and the number of counted words remains unchanged. When a non-blank character is read in, the number of counted words is increased by 1, and the IN_WORD value is 1. When the status value is 1, even if it is read If non-whitespace characters are removed, the number of word statistics does not change.

Below, post the code


#define IN_WORD 1
#define OUT_WORD 0

void main (void)
    int nc,nw,nl;
    char c,word_flag;
    nc = nw = nl = 0;
    word_flag = OUT_WORD;
    while ((c = getchar()) != EOF) {
        nc ++;
        if (c == '\n') {
            nl ++;
        if (!isspace(c) && word_flag == OUT_WORD) {
            nw ++;
            word_flag = IN_WORD;
        } else if (isspace(c) && word_flag == IN_WORD) {
            word_flag = OUT_WORD;
    printf("%d\t%d\t%d\n", nc, nw, nl);

The above code is still very simple. The three variables nc, nw, and nl count the number of characters, the number of words, and the number of lines respectively. Word_flag is used to record status. There are two types of status, IN_WORD and OUT_WORD.

Next, let’s test the above code. The following is a text:

Product-minded engineers are developers with lots of interest in the product itself. 
They want to understand why decisions are made, how people use the product, and love to be involved in making product decisions.
They're someone who would likely make a good product manager if they ever decide to give up the joy of engineering. 
I've worked with many great product-minded engineers and consider myself to be this kind of developer. 
At companies building world-class products, product-minded engineers take teams to a new level of impact.

The above text has a total of 86 words and a total of five lines.

# cat 1.txt | ./a.out 
542 86 5

You can see that the program can count the number of characters, words and lines normally.

The above is the detailed content of Use c language to write wc command - count the number of characters, words and lines. For more information, please follow other related articles on the PHP Chinese website!

The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn