读书人

一个C++小题目,该怎么解决

发布时间: 2012-09-18 16:21:42 作者: rapoo

一个C++小题目
编写一个程序,读取键盘输入,知道遇到@符号为止,并回显输入,同时将大写字符转换为小写,将小写字母转换为大写。
下面这是一份答案,这份答案有什么不足,或者还有个更好的思路吗?

C/C++ code
#include <iostream>#include <cctype>int main(){    using namespace std;    char ch;    cout << "请输入:";    cin.get(ch);    while (ch != '@')    {        if (islower(ch))            ch = toupper(ch);        else if (isupper(ch))            ch = tolower(ch);        cout << ch;        cin.get(ch);    }    return 0;}


[解决办法]
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
char ch;

cout << "请输入:";
while(cin.get(ch), ch != '@')
{
if(islower(ch))
ch = toupper(ch);
else if(isupper(ch))
ch = tolower(ch);
cout<<ch;
}
return 0;
}

读书人网 >C++

热点推荐