也很容易,用指针去遍历字符串,遇到左括号就计数增加,右括号就减少,最后为零就匹配。
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Please input the string:");
char str[101],*ptr=str;
int detected=0;
gets(str);
while(*ptr!='\0')
{
switch(*ptr)
{
case '(':detected++;break;
case ')':detected--;break;
}
ptr++;
}
if(detected==0) printf("The result is:parentheses match!\n");
else printf("The result is:parentheses do not match!\n");
return 0;
}