In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the cases of using c language to write wc commands to count characters, words and lines. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
We know that there is a very common command on the linux operating system, the wc command used to count characters, words, and lines. Use c language to write a program with similar function
When writing this program, you need to master the use of two functions, getchar () and putchar ().
Getchar is used to read a character from standard input, while putchar prints a character to standard output. It is relatively simple to count the number of standard input characters. As long as the getchar function can still read characters, the variable for 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 incremented 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 character read is a blank character (spaces, horizontal tabs and newline characters are all blank characters), the IN_ word value is 0, and the number of words counted remains the same. When a non-blank character is read, the number of statistical words plus 1 is 1, when the state value is 1. Even if non-white space characters are read, the number of words does not change.
Next, post the code
# include # include # define IN_WORD 1#define OUT_WORD 0void main (void) {int nc,nw,nl; char c writing words; 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 very simple, nc,nw,nl three variables to count the number of characters, words and lines. Word_flag is used to record the state, which can be divided into two types, IN_WORD and OUT_WORD.
Next, let's test the above code. The following is a passage:
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 喜悦 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 five lines.
# cat 1.txt |. / a.out 542 865
As you can see, the program can count the number of characters, words and lines normally.
The case of using C language to write wc command to count characters, words and lines is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.