C language: write a program that reads the C source code from the standard input (terminal) and verifies that all curly braces appear correctly in pairs.

Source: Internet
Author: User

C language: write a program that reads the C source code from the standard input (terminal) and verifies that all curly braces appear correctly in pairs.
Write a program that reads the C source code from the standard input (terminal) and verifies that all curly braces appear correctly in pairs. For example, input: {}{}. The match is successful. Input: {}{}}, successful match input: {}}{, matched failed input: }}{{, matched failed input :{}}, failed match input: }{}{{, the match is not successful. Solution: Program:

# Include <stdio. h> # include <stdlib. h> int main () {int count = 0; char ch; printf ("enter a set of curly braces:"); while (ch = getchar ())! = EOF) // use Ctrl + Z to end {if (ch = '{') {count ++;} else if (ch = '}') {if (count = 0) {printf ("Mismatch failed \ n") ;}else {count -- ;}} if (count = 0) {printf ("matched \ n");} else {printf ("matched \ n ");} printf ("the excess '{' is % d \ n", count); system ("pause"); return 0 ;}

 

Result 1: Enter a set of curly braces: {{{}{}^ Z. If the matching fails, the excess '{' is three. Press any key to continue... result 2: enter a set of curly braces: {}}} ^ extra strings '{' that match successfully are 0. Press any key to continue...

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.