我正在尝试创建一个在输入“ ^”字符后大写字符串中下一个字符的函数。代码如下:
void decodeshift( string orig, string search, string replace )
{
size_t pos = 0;
while (true) {
pos = orig.find(search, pos);
if(pos == string::npos)
break;
orig.erase(pos, search.length());
orig.replace(pos, search.length(), replace);
cout<<orig<<endl;
}
}
int main()
{
string question = "What is the message? ";
string answer = "The real message is ";
string shift="^";
string test="a";
string answer1;
//output decoded message
string answer2;
cout << question;
cin >> answer1;
cout << "decoding . . . " << "\n";
//decodeback(answer1, back);
decodeshift(answer1, shift, test);
return 0;
}
我的输入将是:
^hello
所需的输出:
Hello
当前输出
aello
我似乎找不到要使用的正确函数,而我对在这种情况下如何使用toupper感到困惑。我只需要找到合适的替代品。
答案 0 :(得分:0)
尝试更多类似的方法:
#include <cctype>
void decodeshift( string orig, string search )
{
size_t pos = orig.find(search);
while (pos != string::npos)
{
orig.erase(pos, search.length());
if (pos == orig.size()) break;
orig[pos] = (char) std::toupper( (int)orig[pos] );
pos = orig.find(search, pos + 1);
}
return orig;
}
...
answer1 = decodeshift(answer1, "^");
cout << answer1 << endl;
或者,只需删除shift
参数:
#include <cctype>
string decodeshift( string orig )
{
size_t pos = orig.find('^');
while (pos != string::npos)
{
orig.erase(pos, 1);
if (pos == orig.size()) break;
orig[pos] = (char) std::toupper( (int)orig[pos] );
pos = orig.find('^', pos + 1);
}
return orig;
}
...
answer1 = decodeshift(answer1);
cout << answer1 << endl;