Change symbols in the string

Q: "in a given string change all the entries of '$' to '*' until the first entry of '?' and delete all the '@' after '?' "

it also says that you need to use <string>

honestly... this question is confusing. could you help me with it, please?
Last edited on
Probably best to just demonstrate with an example.

So an input of "$$?@@" becomes "**?"
An input of "$@$?@$@" becomes "*@*?$"

When forming the second string, I suggest just starting with an empty string.
Iterate over every character of the first string, and have a bool for whether or not the '?' character has been seen yet.

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
std::string before = "$$?@@";

std::string after; // empty string

bool question_mark_seen = false;
for (char ch : before)
{
    if (!question_mark_seen)
    {
        // pre question mark logic
       if (ch == '?')
       {
           question_mark_seen = true;
           after += ch;
       }
       else if (ch == '$')
           after += "*";
       else
            after += ch;
    }
    else
    {
        //post question mark seen
        // ...
    }
}
Last edited on
thank you so much!)
Hello laura fidarova,

laura fidarova wrote:
it also says that you need to use <string>

Does this mean that you are to use a "std::string" to hold what you need to work on? Or is it also meaning that you are to use the member functions of the string class to achieve the proper result?

Ganado 's solution although very good may not be the correct solution.

Andy

P.S. I am thinking that you may need to use the "string.find" or one of the other finds and "string.replace".
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <iostream>
#include <algorithm>

int main()
{
	//std::string test {"$@$?@$@"};
	std::string test {"$$?@@"};

	const auto at {std::find(test.begin(), test.end(), '?')};

	if (at != test.end()) {
		std::replace_if(test.begin(), at, [](char ch) {return ch == '$'; }, '*');
		test.erase(std::remove_if(at, test.end(), [](char ch) {return ch == '@'; }), test.end());
	}

	std::cout << test << '\n';
}

i'm late, but thank you all so much:)
Topic archived. No new replies allowed.