The second small project in software engineering -- wc, the second small wc in software engineering

Source: Internet
Author: User

The second small project in software engineering -- wc, the second small wc in software engineering

Github source code and engineering file address:Https://github.com/HuChengLing/wc

Basic requirements: the basic function of wc is to collect statistics on the number of characters, words, and lines in a file.

Main functions:The number of characters, words, and lines in the file, and comments.

Design Philosophy: Read a line of Characters in the file to count the number of rows, and then use the character array to count the number of characters and the number of words respectively.

Program code:

# Include <stdlib. h> # include <stdio. h ># include <string> void signalcount (char * Filename); void main () {int c = 0, w = 0, l = 0; int signline = 0; FILE * fp; char fileName [80]; char ch [1024]; // bool inword = false; // In a word, inward equals to ture printf ("please input fileName: "); gets_s (fileName); fp = fopen (fileName," r "); if (fp = NULL) {printf ("can't open file % s \ n", fileName); getchar (); exit (1) ;}while (! Feof (fp) {fgets (ch, 1024, fp); l ++; int length = strlen (ch); for (int I = 0; I <length; I ++) {// if it is a space or not, execute I ++ in the while clause to determine whether the next space appears or end if (ch [I]! = '') {W ++; // if (ch [I]> '0' & ch [I] <'9' | ch [I]> 'A' & ch [I], 'Z' | ch [I]> 'A' & ch [I] <'Z') & ch [I]! = '') // C ++; while (ch [I]! = ''& Ch [I]! = '\ 0 ') {if (ch [I]> '0' & ch [I] <'9' | ch [I]> 'A' & ch [I], 'Z' | ch [I]> 'A' & ch [I] <'Z') & ch [I]! = ''& Ch [I]! = '\ N') c ++; I ++ ;}}} printf ("Total: % d rows % d Words % d characters", l, w, c); signalcount (fileName); fclose (fp); system ("pause");} void signalcount (char * filename) {int len = 0, I = 0, signline = 0; FILE * fp; char str [1000]; fp = fopen (filename, "r"); if (fp = NULL) {printf ("add content to the file \ n"); exit (-1);} while (! Feof (fp) {fgets (str, sizeof (str), fp); len = strlen (str); for (I = 0; I <len; I ++) {if (str [I] = '/' & str [I + 1] = '/') | (str [I] = '/' & str [I + 1] = '*') {signline ++; break ;}}} fclose (fp); printf ("comment row % d row \ n", signline );}

Although I wrote some code myself, I also borrowed a lot of online code. In addition, there are still bugs that cannot read Chinese characters that need to be improved.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.