Need help with strings

Hi, I need help with strings. I want to be able to search for a specific character, like "a". And if that character exist, I want it to get replaced by another character, which I get to decide. Is this possible?

An example would be if the user typed in "Awesome" and I had made it so e gets replaced with a b. So it would write out "Awbsomb".


closed account (48T7M4Gy)
It is all possible. Here are a couple of the string functions available. Check out <string> for more detail.

http://www.cplusplus.com/reference/string/string/at/
http://www.cplusplus.com/reference/string/string/length/
Last edited on
use std::replace:
1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string in = "Awesome";
    std::replace(in.begin(), in.end(), 'e', 'b');
    std::cout << in;
}
Awbsomb
I hope it will help you a little. Run this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

std::string getString();

int main( )
{
    // Get string
    std::string s = getString();

    // Show string
    std::cout << "You entered: " << s << std::endl;

    return 0;
}

std::string getString()
{
    std::cout << "Enter a string: ";
    std::string str;
    std::cin >> str;
    return str;
}
Topic archived. No new replies allowed.