//下面函数检查给定的字符串s是否满足下列两个条件: //1. 字符串s中左括号"("的个数与右括号")"的个数相同。 //2. 从字符串首字符起顺序检查 s 中的字符的过程中,遇到的右括号")"的个数在任何时候均不超过所遇到的左括号"("数。 //若字符串同时满足上述条件,则函数返回非0 值,否则返回0 值。 #include <stdio.h> int check(char *s) { int left,right; left=right=0; while(*s++) { if(*s==‘(‘)left++; else if(*s==‘)‘){ right++; if(right>left) return 0; } } return !(left-right); } int main() { char str[80]; gets(str); int i; i=check(str); if(i!=0) printf("括号数匹配。"); else printf("括号数不相匹配。"); }
运行结果:
原文:https://www.cnblogs.com/yanglike111/p/13912343.html