2013-03-04 122 views
1

最後我找到解決方案轉換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(); 
} 

如何改善上面的代碼來獲得字符串,而不是一個單個字符?循環保持多次打印相同的語句。由於

+0

什麼是「循環保持多次打印相同的語句」? – Asha 2013-03-04 08:23:29

+0

一個無關的錯誤:你不能用'char'調用''中的函數。您必須首先轉換爲'unsigned char',否則會遭受未定義的行爲。 – 2013-03-04 08:33:39

+1

@JamesKanze,你有一些文件表明轉換爲unsgined字符確實是必需的嗎?我找到了[tolower()參考](http://www.cplusplus.com/reference/cctype/tolower/),其中使用了'char'。 – kamituel 2013-03-04 08:58:31

回答

2

更新:這裏是更清潔的解決方案,它輸出一個單個單詞。

#include <cctype> 
#include <iostream> 
#include <algorithm> 
using namespace std; 

char switch_case (char ch) { 
    if (isalpha (ch)) { 
     if (isupper (ch)) { 
     return tolower (ch); 
    } 
    else { 
     return toupper (ch); 
    } 
    } 
    return '-'; 
} 

int main() 
{ 
    string str; 
    cout<<"Enter a word: "; 
    cin >> str; 

    transform(str.begin(), str.end(), str.begin(), switch_case); 
    cout << str << "\n"; 
} 

本例中使用的是std::transform


只需讀取整個字,並使用std::string::iterator以一次遍歷一個字母:

#include <cctype> 
#include <iostream> 
using namespace std; 

int main() 
{ 
    string str; 
    cout<<"Enter a word: "; 
    cin >> str; 

    for (string::iterator it = str.begin(); it != str.end(); ++it) { 
    char ch = *it; 
    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(); 
} 
+0

沒有工作..只將第一個字母轉換爲upercase,如果一個字符串包含任何小寫,它只是將第一個字母轉換爲大寫..需要將所有的低位轉換爲高位 – 2013-03-04 08:28:23

+0

不正確。整個單詞正在轉換,因爲我沒有真正改變你的算法,只是添加了一個for循環。 – kamituel 2013-03-04 08:30:19

+0

結果: 輸入一個字符:sasas 大寫字母相當於S – 2013-03-04 08:31:56

2

杉杉使用輸入操作符讀入一個字符串:

std::string input; 
std::cin >> input; 

可選擇使用std::getline獲得比單個字。您可以使用std::transform將字符串轉換爲大寫或小寫。

您也可以使用range-based for loop遍歷字符串中的字符。

+1

使用'std :: stransform'可能有點棘手,因爲你無法直接傳遞'toupper'或'tolower'。任何處理角色數據的專業程序員都會在他的工具包中使用必要的功能對象,並使用這些對象,但這看起來更像是一個學習練習,而不是專業程序員在他的工作中遇到的問題。 – 2013-03-04 08:35:40

1

C++ 11:

#include <cctype> 
#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    string s; 
    cout << "Enter data: "; 
    cin >> s; 

    for (auto &ch : s) 
    { 
     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(); 
} 

#include <cctype> 
#include <iostream> 
#include <string> 
#include <algorithm> 

using namespace std; 
int main() 
{ 
    string s; 
    cout << "Enter a string: "; 
    cin >> s; 

    transform(s.begin(), s.end(), s.begin(), [](char ch) 
    { 
     return isupper(ch)? tolower(ch) : toupper(ch); 
    }); 
} 

如果您有g++試試:g++ test.cpp -o test -std=c++11進行編譯。