replace vowels with undescore sr.at(i)

In this program, you ask user to enter a sentence and read it using string class function getline. Use a for loop to iterate through all characters. Please use sr.ta(i) method to access each string character. If a character is a vowel, replace it with "_". Both upper and lower case vowels have to be replaced, Those who use an array for string are going to lose points as char.at(i) verifies that the character position in a string is valid.
Example run:
Please enter a sentence:
Hello There!
You entered the sentence "Hello There!"
Your sentence after replacing vowels by underscore is H_ll_ Th_r_!

The program that I use is Dev C++ 6.3
Last edited on
One of three zero effort homework dumps in an hour.
https://www.cplusplus.com/forum/general/277554/
https://www.cplusplus.com/forum/general/277551/

If you're serious about learning, you really need to make an effort.
As C++17:

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

int main()
{
	const char* const vowels {"aeiouAEIOU"};
	std::string sent;

	std::cout << "Please enter a sentence: ";
	std::getline(std::cin, sent);

	std::cout << "\nYou entered the sentence " << sent;

	for (size_t c = 0; c < sent.size(); ++c)
		if (auto& ch {sent.at(c)}; std::strchr(vowels, ch) != nullptr)
			ch = '_';

	std::cout << "\nYour sentence after replacing vowels by underscore is " << sent << '\n';
}



Please enter a sentence: Hello There!

You entered the sentence Hello There!
Your sentence after replacing vowels by underscore is H_ll_ Th_r_!

Topic archived. No new replies allowed.