我是編程的初學者,因此如果我以錯誤的方式處理問題,請儘量簡單。我正在做這個任務。我的目的是從用戶處取出一個字符串,並用另一個符號替換所有的字符。下面的代碼應該可以找到所有的As,然後用* s代替。我的代碼顯示完全意外的結果。還有_deciphered.length()的目的是什麼。如何使用特定符號查找並替換字符串中的所有字符C++
例如: 「我是一個壞男孩」應該變成「我* M * B * d男孩」
那麼我應該實現它所有的大寫和小寫字母和數字,並與更換不同的符號,反之亦然,使一個小編碼 - 解碼方案
#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;
}
這僅適用直到它找到第一個也是唯一一個返回字。例如,如果我輸入「bad bay」,它會返回「b * d」。我如何得到它返回完整的字符串「b * d b * y」? –
@UsamaKhurshid不,你錯了。它會用'*'替換所有的'A'字符。 – juanchopanza
@UsamaKhurshid我試過了。要麼你正在嘗試其他的東西,或者你的std庫實現被破壞了。 – juanchopanza