我是编程初学者,所以如果我以错误的方式解决问题,请放轻松。我这样做是作为一项任务。我的目的是从用户那里获取一个字符串并用另一个符号替换所有字符。下面的代码应该找到所有的 As 并用 *s 替换。我的代码显示完全出乎意料的结果。还有 _deciphered.length() 的目的是什么。
例如:“I Am A bAd boy”应该变成“I *m * b*d boy”
然后我应该为所有大写和小写字母和数字实现它,并用不同的符号替换,反之亦然,以制作一个小的编码解码程序
#include <iostream>
#include <string>
using namespace std;
string cipher (string);
void main ()
{
string ciphered, deciphered;
ciphered="String Empty";
deciphered="String Empty";
cout<<"Enter a string to \"Encode\" it : ";
cin>>deciphered;
ciphered=cipher (deciphered);
cout<<endl<<endl;
cout<<deciphered;
}
string cipher (string _deciphered)
{
string _ciphered=(_deciphered.replace(_deciphered.find("A"), _deciphered.length(), "*"));
return _ciphered;
}