[英]checking for two underscores next to each other
我的代码应该检查两个下划线是否彼此相邻,在字符串的开头还是结尾以及其他更多规则.....问题是当代码在整个字符串中找到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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.