2017-05-06 25 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

これは、最初の文字だけを変換して、example input => hellOWOrLD.hELLOWORLDのように、単語の他の部分を小文字にすることです。出力=> Helloworld。こんにちは世界。 –

答えて

0

私はisalpha()toupper()tolower()

EDITを使用します。句読点を探すために。

#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

これはあなたの質問に答えますか? –

関連する問題