我想写一个程序来获取一个字符,然后检查并打印它是否使用大写或小写。然后,我希望它保持循环,直到用户输入一个应该产生消息的“0”。不起作用的是底部的条件,这个条件似乎从未得到满足。这个循环不符合条件时结束
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch,nope; // Int ch and nope variable used to terminate program
do // start 'do' loop to keep looping until terminate number is entered
{
printf("Enter a character : "); // asks for user input as a character
scanf("%c",&ch); // stores character entered in variable 'ch'
if(ch>=97&&ch<=122) { // starts if statement using isupper (in built function to check case)
printf("is lower case\n");
} else {
printf("is upper case\n");
}
}
while(ch!=0); // sets condition to break loop ... or in this case print message
printf("im ending now \n\n\n\n\n",nope); // shows that you get the terminate line
}
'CH = 0'是错误的,应该是' ch!='0'' – Maroun 2013-03-17 21:53:17
幻数97和122是个不错的主意。使用''a''和''z'',或者只需调用''中声明的'islower()'。此外,代码还会为数字和标点符号报告“大写”。 –
2013-03-17 22:20:55