A method of counting the number of lines and words entered in C programming

  • 2020-05-05 11:37:31
  • OfStack

counts the number of rows entered

The standard library ensures that the input text stream appears as a sequence of lines, each of which ends with a newline character. Therefore, the number of statistical lines is equivalent to the number of statistical newline characters.


#include <stdio.h>
/* count lines in input */
main()
{
 int c, nl;
 nl = 0;
 while ((c = getchar()) != EOF)
 if (c == '\n')
  ++nl;
 printf("%d\n", nl);
}

In this program, the body of the while loop is an if statement that controls the self-incrementing statement ++nl. The if statement first tests the condition in the parentheses and, if true, executes the statement that follows (or the set of statements enclosed in curly braces). Again, indentation is used to indicate the control relationship between statements.

The double equals sign == is an operator in the C language that indicates an equal relationship (similar to the single equals sign = in Pascal and.EQ in Fortran). Since the C language USES the single equals sign = as the assignment operator, the double equals sign == is used to indicate the logical relationship of equality. It is important to note that beginners to the C language sometimes make the mistake of writing the single equals sign as = when expressing the logical relationship of equals (== should be used). As we'll see later, even if this is misused, the result is usually still a valid expression, so the system doesn't give a warning.

A character in a single quote represents an integer value equal to the value of the character in the machine character set, which we call a character constant. However, it is just another way of writing small integers. For example, 'A' is a character constant; The value in the ASCII character set is 65 (that is, the internal representation of the character A is 65). Of course, 'A' is better than '65' because. The meaning of 'A' is clearer and independent of a particular character set.

The escape character sequence used in string constants is also a valid character constant; for example, '\n' represents the value of the newline character, which is 10 in the ASCII character set. We should note that '\n' is a single character, which in an expression is nothing more than an integer; And "\n" is a string constant that contains only one character.

Write a program that counts the number of Spaces, tabs, and newlines.


#include <stdio.h>

main()
{
 /* blanks, tabs, and newlines */
 int c, nb, nt, nl;

 nb = 0;
 nt = 0;
 nl = 0;

 while( (c = getchar()) != EOF)
 {
 if(c == ' ')
  ++nb;
 if(c == '\t')
  ++nt;
 if(c == '\n')
  ++nl;
 }
 printf("%d %d %d \n", nb, nt, nl);
}

counts the number of words it inputs

A word is loosely defined here as any sequence of characters that does not contain Spaces, tabs, or line breaks. The following program is the backbone of the wc program on the UNIX system:


#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
main()
{
 int c, nl, nw, nc, state;
 state = OUT;
 nl = nw = nc = 0;
 while ((c = getchar()) != EOF) 
 {
 ++nc;
 if (c == '\n')
  ++nl;
 if (c == ' ' || c == '\n' || c == '\t')
  state = OUT;
 else if (state == OUT) {
  state = IN;
  ++nw;
 }
 }
 printf("%d %d %d\n", nl, nw, nc);
}

As the program executes, each time the first character of a word is encountered, it is counted as a new word. The state variable keeps track of whether the program is currently in a word, and its initial value is "not in the word," that is, the initial value is assigned to OUT. We use the symbolic constants IN and OUT instead of the corresponding values 1 and 0 to make the program easier to read. In smaller programs, this may not seem like an advantage, but in larger programs, if you do it from the beginning, the added effort is worth the benefit of improved readability. Readers will also find it relatively easy to make a lot of changes to a program if the magic Numbers are all in the form of symbolic constants.

The following statement nl = nw = nc = 0; Three of the variables nl, nw, and nc are set to 0. This usage is common, but note the fact that in expressions that have both value and assignment functions, the assignment combination order is right-to-left. So this is the same thing as n1 = (nw = (nc = 0));

The operator || represents OR (logical or), so the following statement if (c == '|| c== '\n' || c == '\t') means "if c is a space, or c is a newline, or c is a TAB". Accordingly, the operator && represents AND (logical and), which is only one priority higher than ||. Expressions connected by && or || are evaluated from left to right, and the evaluation is terminated as soon as the final result is true or false. If c is a space, there is no need to test whether it is a newline or TAB, so that you do not have to perform the next two tests. This is not particularly important here, but it is necessary in some more complex cases, as we shall see shortly.

This program also includes an else section that specifies the action to perform when the conditional part of the if statement is false. Its general form is


if ( The expression of type )
  statements  1
else
  statements  2

Of the two statements in if-else, one and only one is executed. If the value of the expression is true, then statement 1 is executed, otherwise statement 2 is executed. Both statements can be either a single statement or a sequence of statements enclosed in curly braces. In a word-counting program, the statement after else is still an if statement, which controls the two statements contained within curly braces.


Related articles: