最後我找到解決方案轉換lowecase爲大寫,並確定該字符串是否是字母或數字代碼如下:C++通過字符串循環
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character: ";
gets(ch);
if (isalpha (ch)) {
if (isupper (ch)) {
ch = tolower (ch);
cout<<"The lower case equivalent is "<< ch <<endl;
}
else {
ch = toupper (ch);
cout<<"The upper case equivalent is "<< ch <<endl;
}
}
else
cout<<"The character is not a letter"<<endl;
cin.get();
}
如何改善上面的代碼來獲得字符串,而不是一個單個字符?循環保持多次打印相同的語句。由於
什麼是「循環保持多次打印相同的語句」? – Asha 2013-03-04 08:23:29
一個無關的錯誤:你不能用'char'調用''中的函數。您必須首先轉換爲'unsigned char',否則會遭受未定義的行爲。 –
2013-03-04 08:33:39
@JamesKanze,你有一些文件表明轉換爲unsgined字符確實是必需的嗎?我找到了[tolower()參考](http://www.cplusplus.com/reference/cctype/tolower/),其中使用了'char'。 – kamituel 2013-03-04 08:58:31