编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。
分析:编写这个程序的思路是,当‘{’出现时计数器加1,当‘}’出现时,计数器减1,最后判断,当计数器为0时,{}是成对出现的,但是一定要注意的是当‘}’出现并且计数器为0时,一定是不匹配的。
代码如下:
int main(){
int ch = 0;
int count = 0;
while ((ch=getchar()) != EOF)
{
if(ch == '{')
{
count++;
}
else if (ch == '}'&&count == 0)
{
printf("不匹配!");
return 0;
}
else if (ch == '}')
{
count--;
}
}
if (count == 0)
{
printf("匹配!");
}
else
{
printf("不匹配 !");
}
system("pause");
return 0;
}