【c语言】从标准输入读取c源代码,并验证所有花括号都成对出现

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int ch;
	int braces;

	braces = 0;
	while( (ch = getchar()) != EOF )
	{
	     if(ch == '{')
			 braces += 1;
		 if(ch == '}')
			 if(braces == 0)
				 printf("extra closing brace!\n");
			 else
				 braces -= 1;
	}
	if(braces > 0)
		printf("%d unmatched opening brace!\n",braces);

    return 0;
}

你可能感兴趣的:(【c语言】从标准输入读取c源代码,并验证所有花括号都成对出现)