I need to count all vowels and replace it with the letter x, in a textbox. I managed to do the counting part and here's the code, but i'm having problem with replacing all vowels in the textbox with the letter x. Can someone help me?
int total = 0;
string sentence = textBox1.Text.ToLower();
char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y'};
{
total++;
}
total = sentence.Count(c => vowels.Contains(c));
MessageBox.Show("There are " + total++ " vowels";
for (int i = 0; i < sentence.Length; i++)
EDIT 1:
Thank you all for helping. For some reason the vowels in my textbox doesnt change!!! It does the counting but no replacement of the letter x. I've tried all the solutions here, but still nothing happens to my the vowels in the textbox.
foreach(char vowel in vowels)
sentence = sentence.Replace(vowel, 'x');
For some reason the vowels in my textbox doesnt change!!! It does the counting but no replacement of the letter x. I've tried all the solutions here, but still nothing happens to my the vowels in the textbox.
The textbox and the string are not linked with each other. So if you change the string you won't change the TextBox.Text
. You have to re-assign the new value:
textBox1.Text = sentence; // after you have used Replace like shown above
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With