将小写转换为大写的第一个字符,并使其他字符保持较低

时间:2017-05-06 10:25:01

标签: c++

 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。为HelloWorld。

它应该像图片中那样工作......

enter image description here

1 个答案:

答案 0 :(得分: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';
}