How to count and count the number of lines in C language programming _c language

Source: Internet
Author: User
Tags numeric value

Statistics the number of rows entered

The standard library guarantees that the input text stream appears as a row sequence, with each line ending with a newline character. Therefore, the statistic line number is equivalent to counting the number of line breaks.

#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 loop body of the While loop statement is an if statement that controls the ++nl of the increment statement. The IF statement tests the condition in parentheses, and if the condition is true, executes the following statement (or a set of statements enclosed in curly braces). Again, the control relationship between statements is shown by indentation.

The double equals number = = is the operator in the C language that represents the "equals" relationship (similar to the single equals number in Pascal = and Fortran). EQ.). Because the C language will be the single equals number = as the assignment operator, use the double equals number = = to represent the logical relationship of equality to differentiate. Note here that, in the expression "equals" the logical relationship (should use = =), C language beginners sometimes mistakenly written as a single equals number =. We will see later that even if this is misused, the result is usually still a valid expression, so the system does not give a warning message.

The characters in single quotes represent an integer value equal to the numeric value of this character in the machine character set, which we call the constants quantity. However, it is just another way of writing a small integer number. For example, ' A ' is a word constants, and in the ASCII character set its value is 65 (that is, the internal representation value of character A is 65). Of course, it's better to use ' A ' than 65, because. The meaning of ' a ' is clearer and irrelevant to a particular character set.

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

Next, write a program that counts the number of spaces, tabs, and line breaks.

#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);


Count the number of words entered

The definition of a word is loosely defined, which is any sequence of characters that does not contain spaces, tabs, or line breaks. The following program is the backbone of the WC program in UNIX systems:

#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 = = ' | | | = = ' \ n ' | | c = = ' \ t ') state
  = out;
 else if (state = = off) {state
  = in;
  ++NW
 }
 }
 printf ("%d%d%d\n", NL, NW, NC);
}

When a program executes, it is counted as a new word whenever it encounters the first character of the word. The state variable recorder is currently in a word and its initial value is "not in the word", which means that the initial value is assigned out. We use symbolic constants in and out here instead of using their corresponding values 1 and 0, so the program is easier to read. In smaller programs, this practice may not see any advantage, but in larger programs, if you do this from the start, the increased workload is worth the benefit of improving the readability of the program. Readers will also find that if the magic numbers in a program are in the form of symbolic constants, it is much easier to make a large number of changes to the program.

The following statement is NL = NW = NC = 0; The 3 variables, NL, NW, and NC, will be set to 0. This usage is common, but pay attention to the fact that in an expression with a value and an assignment of two functions, the assignment binding order is from right to left. So the above statement is equivalent to N1 = (NW = (NC = 0));

operator | | Represents or (logical OR), the following statement if (c = = ' | | | c== ' \ n ' | | | c = = ' t ') means "if C is a space, or C is a newline character, or C is a tab character (previously, escape sequence \ t is a visible representation of tab characters). Accordingly, the operator && represents and (logic with), it is only more than | | A higher priority. by && or | | The expression of the connection is evaluated from left to right, and the evaluation is terminated immediately if the final result is judged to be true or false in the evaluation process. If C is a space, there is no need to test whether it is a newline character or a tab, so you do not have to perform the next two Tests. This is not particularly important here, but it is necessary to do so in some more complex situations, and we will soon see this example.

This program also includes an else part that specifies the action to be performed if the conditional part in the IF statement is false. Its general form is:

if (expression)
 statement 1
Else
 statement 2

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

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.