【基础学习】C 验证输入的}{是否正确的成对出现
2016-12-30 本文已影响3人
Jiubao
挺有意思的题目,解题如下:
#include <stdio.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 braces!\n", braces);
}
return 0;
}