C++ change letters to asterisk

I want to make this with int replace_char function, but i dont know what to change in this to make it work....I'm stuck...

should look like this - https://imgur.com/a/dbIrUYB

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
  int main () {

string str;
char s_char;

cout<< "Enter text here:" <<endl;
string code;
cin >> code;

cout<< "You entered:" <<endl;
cout<< code << endl;


cout<< "Choose symbol to replace with *:" <<endl;

cin>> s_char;


cout<< Text: <<endl;




return 0;
}
Last edited on
what int (the return value, what is it?)
Hello frog1990,

A little rework of your code:
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>

int main()
{
    std::string str;  // <--- Not used at this point.
    char symbol{};    // <--- Initialize variable. A good name makes the code more understandable.

    std::string code;
    
    std::cout << "Enter text here: ";
    cin >> code;    

    std::cout << "You entered: ";
    std::cout << code << '\n';

    std::cout << "Choose symbol to replace with *: ";
    cin >> symbol;

    std::cout << Text: << '\n';

    return 0;  // <--- Not required, but makes a good break point.
}

I have not tested this yet and line 20 will be an error.

You do not need all the blank lines that you have. It makes it look like there is something missing.

It is also best to include the header files with your code, so that it can be compiled and tested. Also others can check to see if you have included something that you do not need or is you have missed something.

Try and prefer using the new line (\n) over the "endl" whenever possible. The "endl" is a function that takes time to call and complete. The more you have you may notice the program running slower.

A prompt tends to work better as: std::cout << "Enter text here: ";. Without the "\n" or "endl" the "cin" is on the same line as the prompt. The "cin" following the "cout" will flush the output buffer before the "cin" will take any input.

After you fix line 20 you could go through "code" 1 character at a time and if it matches "symbol" replace it with the "*". Or use the strings find function to find every occurrence. As Furry Guy mentioned.

Andy
It's probably better to use getline() rather than >> to obtain the text so that white space chars can also be entered. Also replace_if () is easier than find_first_of and replace().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	const char newchar {'*'};
	char symbol {};
	std::string code;

	std::cout << "Enter text:\n";
	std::getline(std::cin, code);

	std::cout << "\nYou entered:\n";
	std::cout << code << '\n';

	std::cout << "\nChoose symbol to replace with " << newchar << " : ";
	std::cin >> symbol;

	std::replace_if(code.begin(), code.end(), [symbol](char ch) {return ch == symbol; }, newchar);
	std::cout << "\nText:\n" << code << '\n';
}

Last edited on
Thanks!
And if i would need change upper and lower case letters at the same time?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main ()
{
   string str;
   char c;
   cout << "Enter text:\n";
   getline( cin, str );
   cout << "\nYou entered:\n" << str << '\n';
   cout << "\nChoose symbol to replace with *: ";
   cin >> c;
   for ( char &d : str ) if ( tolower( d ) == tolower( c ) ) d = '*';
   cout << str << '\n';
}
Hello frog1990,

If I understand your link, what you want is the first letter of each word to be a capital letter.

If this is incorrect you will need to explain better what you mean by:

And if i would need change upper and lower case letters at the same time?



As I was thinking this can be easily done in a for loop as you iterate over each element of the string. You will also need to check the variable used as the index to the string to deal with the first element of the string.

Andy
first letter of each word to be a capital letter


Assuming this, consider:

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
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

int main()
{
	const char newchar {'*'};
	char symbol {};
	std::string code;

	std::cout << "Enter text:\n";
	std::getline(std::cin, code);

	std::cout << "\nYou entered:\n";
	std::cout << code << '\n';

	std::cout << "\nChoose symbol to replace with " << newchar << " : ";
	std::cin >> symbol;

	std::istringstream iss(code);
	std::string newstr;

	for (std::string wd; iss >> wd; newstr += wd + (iss.peek() != EOF ? " " : ""))
		for (size_t ind = 0; auto& ch : wd) {
			if (ch == symbol)
				ch = newchar;

			ch = ind++ ? (char)std::tolower(ch) : (char)std::toupper(ch);
		}

	std::cout << "\nText:\n" << newstr << '\n';
}



Enter text:
hello world!

You entered:
hello world!

Choose symbol to replace with * : o

Text:
Hell* W*rld!

Topic archived. No new replies allowed.