gpt4 book ai didi

c++ - 使用 find 检查,如果我在字符串中有下划线

转载 作者:行者123 更新时间:2023-11-28 02:58:09 24 4
gpt4 key购买 nike

我在我的程序中提出了几个问题,但这是其中之一。我希望我的代码检查一个字符串中是否有两个或多个下划线彼此相邻,例子>

lol__lol, lol___lol, etc...

我应该使用 while 来做吗?这是我的实际代码,它仅适用于在我像这样放置下划线时进行检查>

_lol_ 

所以第一个和最后一个字符是 _ 并且它们不能,所以它有效,因为我得到输出“Chyba - error”

std::string::size_type n;
std::string const ss = slovo;

n = ss.find('_');

// kontrola podtrznika
if ((ss[0] >= 'A' && ss[0] <= 'Z') && ss[1] == '_')
{
cout << "chyba" << endl;
}

if ( !ss.empty() && ss[0] == '_' && ss[ss.length() - 1] == '_' )
{
cout << "chyba" << endl;
}

if ( ss.length() > 3 && ss.find( "__", 1, ss.length() - 2 ) != std::string::npos )
{
cout << "chyba" << endl;
}

if (n == std::string::npos)
{
string s = transform(slovo);
cout << s << endl;
}
else
{
string s = untransform(slovo);
cout << s << endl;
}

最佳答案

除非您在要求中误导了您,否则您的代码似乎过于复杂。

当然以下就足够了:

if (slovo.find("__") != slovo.npos) {
// ...
}

关于c++ - 使用 find 检查,如果我在字符串中有下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21527656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com