检查两个彼此相邻的两个下划线

checking for two underscores next to each other

本文关键字:两个 下划线 检查      更新时间:2023-10-16

我的代码应该检查两个下一个下一个下一个彼此,在字符串的开头还是在字符串的末尾以及更多其他规则.....问题是代码时在整个字符串中找到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);
相关文章: