我的代码应该检查两个下划线是否彼此相邻,在字符串的开头还是结尾以及其他更多规则.....问题是当代码在整个字符串中找到2个下划线时,它会打印Chyba,但是它不应该。它应该仅在找到类似abc__abc的东西时才打印Chyba,但是它像这样abc_abc_abc >> chyba一样工作...有什么解决方案吗?

 void Convert(string input){
    string output = "";
    string flag = "";
    bool underscore = false;
    bool uppercase = false;
    if ( islower(input[0]) == false){
        cout << "Chyba!" <<endl;
        return;
    }

    for (int i=0; i < input.size(); i++){
        if ( (isalpha( input[i] ) || (input[i]) == '_') == false){
            cout << "Chyba!" <<endl;
            return;
        }
        if (islower(input[i])){
            if (underscore){
                underscore = false;
                output += toupper(input[i]);
            }
            else
                output += input[i];

        }
        else if (isupper(input[i])){
            if (flag == "C" || uppercase){
                cout << "Chyba!"<<endl;
                return;
            }
            flag = "Java";
            output += '_';
            output += tolower(input[i]);

        }
        else if (input[i] == '_'){
            if (flag == "Java" || underscore){
            cout << "Chyba!" <<endl;
            return;
            }
            flag = "C";
            underscore = true;
        }

    }


    for (int i=input.size()-1; i >=0; i--){
      if (input[i] == '_'){
            if (flag == "Java" || underscore){
            cout << "Chybaaa!" <<endl;
            return;
            }
            flag = "C";
            underscore = true;
        }
    }
cout << output <<endl;
}

最佳答案

bool containsAdjacentUnderscores = std::search_n(begin(input), end(input), 2, '_') != end(input);

关于c++ - 检查彼此相邻的两个下划线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21563655/

10-09 06:37