2017-05-06 65 views
-2
void correcter(string s, int j) 
{ 
    string correct; 
    for (; j < s.length(); j++) 
    { 
     if (int(s[j]) != 46){ 
      if (int(s[j]) >= 97 && int(s[j]) <= 122 && i == 0) 
      { 
       char a = int(s[j]) - 32; 
       correct += a; 
       i++; 
      } 
      else if (int(s[j]) >= 65 && int(s[j]) <= 90&&i==0) 
      { 
       char a = int(s[j]) + 32; 
       correct += a; 
       i++; 
      } 
      else if (int(s[j]) >= 65 && int(s[j]) <= 90) 
      { 
       char a = int(s[j]) + 32; 
       correct += a; 
       i++; 

      } 
      else 
       correct += s[j]; 
     } 
     else 
     { 
      correct += ". "; 
      i = 0; 
     } 
    } 
    cout << correct << endl; 
} 

問題是編寫一個代碼,將字符串的第一個字符轉換爲大寫字母,而其他字符保持小寫字母。在每一個「。」之後。先把字再放在上面,其他部分再放低!將小寫字母轉換爲大寫字母,並使其他字符保持較低

輸入:

hellOWOrLD.hELLOWORLD。

輸出:

爲HelloWorld。你好,世界。

它應該像圖片...

enter image description here

+0

你的問題是?僅供參考:看看'std :: tolower'和'std :: toupper'。 – Rakete1111

+0

以及它只能轉換第一或所有字符我的是轉換它的第一個字符加上使其他部分的單詞小寫,如示例input => hellOWOrLD.hELLOWORLD。輸出=>爲HelloWorld。你好,世界。 –

回答

0

我會用isalpha()toupper()tolower()

編輯:爲了尋找標點符號。

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

using namespace std; 

void upperCase(string& line) { 
    bool firstCharacter = true; 
    string punctuation=".?!"; 

    for(int i = 0; line[i] != '\0'; i++) { 
     char c=line[i]; 
     if(isalpha(c)) { 
      if (firstCharacter) { 
       line[i] = toupper(c); 
       firstCharacter = false; 
      } else { 
       line[i] = tolower(c); 
      } 
     } else if (punctuation.find(c)!=string::npos) { 
      firstCharacter=true; 
     } 
    } 
} 

int main() { 
    string str = "hello UNiverse?! World? Hello. Hello"; 
    upperCase(str); 
    std::cout << str << '\n'; 
} 
+0

「。」如果有一些它應該與一些空格分開,然後再用大寫字母表示第一個字母 –

+0

我已編輯考慮。 !和? –

+0

這是否回答你的問題? –

相關問題