Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count and replace all vowels in a textbox

Tags:

c#

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.

like image 464
Lini Avatar asked Dec 07 '22 20:12

Lini


1 Answers

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
like image 54
Tim Schmelter Avatar answered Dec 22 '22 04:12

Tim Schmelter