1

I am a beginner in programming so take it easy if i approached the problem in a wrong way. i am doing this as an assignment. My purpose is to take a string from user and replace all the characters with another symbol. The code below is supposed to find all the As and replace then with *s. My code is showing totally unexpected result. Also what is the purpose of _deciphered.length().

for example: "I Am A bAd boy" should turn into "I *m * b*d boy"

then i am supposed to implement it for all capital and small letters and numbers and replace with different symbols and vice versa to make a small Encode-Decode program

#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;
}

3 Answers 3

4

Since you seem to be using the standard library already,

#include <algorithm> // for std::replace

std::replace(_deciphered.begin(), _deciphered.end(), 'A', '*');

If you need to do this by hand, then bear in mind that an std::string looks like a container of char, so you can iterate over its contents, check if each element is 'A', and if so, set it to '*'.

Working example:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
  std::string s = "FooBarro";
  std::cout << s << std::endl;
  std::replace(s.begin(), s.end(), 'o', '*');
  std::cout << s << std::endl;
}

Output:

FooBarro

F**Barr*

Sign up to request clarification or add additional context in comments.

4 Comments

this only works till it finds first a and returns only that word. eg if i enter "bad bay" it returns "bd". how do i get it to return complete string "bd b*y"?
@UsamaKhurshid no, you are mistaken. It will replace all A characters by *.
@UsamaKhurshid I tried it. Either you are trying something else or your std library implementation is broken.
the solution that u posted now works! thanks.. i wonder what was wrong with the first one you posted!
1

You can use std::replace

std::replace(deciphered.begin(), deciphered.end(), 'A', '*');

Also, you can use std::replace_if if you want to replace multiple values that match a certain criteria.

std::replace_if(deciphered.begin(), deciphered.end(), myPredicate, '*');

where myPredicate returns true if the character matches the criteria to be replaced. So for example, if you want to replace both a and A, myPredicate should return true for a and A and false for other characters.

Comments

0

I would personally use regular experssion replace to repace "A or a" with *

Have a look at this answer for some pointers: Conditionally replace regex matches in string

1 Comment

While a regular expression could be used, std::replace is much simpler and has much less overhead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.