3

Ok so basically what I am trying to accomplish here is to count all of the characters in the richtextbox and show which of them are the vowels and it will constantly update and show the number of chars/vowels in the toolstrip bar. Pretty straightforward code but there seems to be some errors in the foreach loop and the textchanged. Would appreciate some feedback please :)

private void Form1_Load(object sender, EventArgs e)
{
    int vowels;
    int characters;
    foreach(int char in rtbDoc)
    {
        characters+=1;
    }
    if (rtbDoc.Text == "a".ToLower() || rtbDoc.Text == "e".ToLower() 
        || rtbDoc.Text == "i".ToLower() || rtbDoc.Text == "o".ToLower()
        || rtbDoc.Text == "u".ToLower())
    {
        vowels += 1;
    }

    toolStripStatusLabel1.TextChanged = characters + 
                      "Characters, of which " + vowels + " are vowels";
}
stuartd
  • 70,509
  • 14
  • 132
  • 163

5 Answers5

7

Here is a Linq approach

char[] Vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
int CharacterCount = rtbDoc.Text.Length;
int VowelCount = rtbDoc.Text.Count(x => Vowels.Any(y => char.ToLower(x) == y));
fubo
  • 44,811
  • 17
  • 103
  • 137
  • Related to make it work with non ASCII characters: http://stackoverflow.com/a/35088029/1207195 (and any other language which is not en-US). Side note: you can directly use .Count(x => ...) instead of .Where(x => ...).Count() – Adriano Repetti Apr 15 '16 at 13:34
  • 1
    Sacrificing readability for terseness - `int VowelCount = text.ToLower().Count(Vowels.Contains);` – stuartd Apr 15 '16 at 13:46
  • And for everyone else thinking about, for example, Turkish language: **do not ever use ToLower()** in your code. 99.9% it's the wrong pick. Use a culture aware StringComparer, always. – Adriano Repetti Apr 15 '16 at 13:56
2

First I suggest extracting a method:

private static bool IsVowel(Char value) {
  //TODO: is 'y' a vowel?
  return 
    value == 'a' || value == 'i' || value == 'e' || value == 'o' || value == 'u' ||
    value == 'A' || value == 'I' || value == 'E' || value == 'O' || value == 'U';
}

Then using Linq:

//TODO: do you really want to compute vowels count on Form Load? Not on rtbDoc.TextChanged?
private void Form1_Load(object sender, EventArgs e) {
  String text = rtbDoc.Text;

  // Finally, formatting which is more readable
  //DONE: you've probably want to assign to Text, not to TextChanged
  toolStripStatusLabel1.Text = 
    String.Format("{0} Characters, of which {1} are vowels.", 
                  text.Length,
                  text.Count(c => IsVowel(c)));
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You have to check each char if it is a vowel

private void Form1_Load(object sender, EventArgs e)
{
  char[] vowelSet = { 'a', 'e', 'i', 'o', 'u' };
  int vowels = 0;
  int characters = 0;
  foreach(char c in rtbDoc.Text.ToLower())
  {
    characters+=1;

    if (vowelSet.Contains(c))
    {
        vowels += 1;
    }
  }

  toolStripStatusLabel1.TextChanged = characters + 
                      "Characters, of which " + vowels + " are vowels";
}

To Update the label after changes, you have to use TextChanged event of the RichTextBox

-1

Your code should be this way if you want it to update every time it changes

 // you code should be inside the TextChanged event for the richtextbox to get called everytime the text is modified
    private void rtbDoc_TextChanged(object sender, EventArgs e) {
        int vowels = 0; // you should initialize your variables
        int characters = 0;// you should initialize your variables
        foreach (char c in rtbDoc.Text) { // you can't name a variable " char ", and you should choose rtbDoc.Text
            characters += 1;
            if (c.ToString().ToLower() == "a" || c.ToString().ToLower() == "e" // ToLower() function should be on the input
                || c.ToString().ToLower() == "i" || c.ToString().ToLower() == "o" // you should check the character not the whole text
                || c.ToString().ToLower() == "u") {
                vowels += 1;
            }
        }// the if part should be inside the foreach loop to check every character 

        toolStripStatusLabel1.Text = characters +           // you should use toolStripStatusLabel1.Text 
                          "Characters, of which " + vowels + " are vowels";
    }
Ayman El Temsahi
  • 2,600
  • 2
  • 17
  • 27
-1

You're almost there.

Your if statement is saying that if the text in the box IS "a" (or any other vowel character), add one. Otherwise, you don't do anything. Take for example, the input "aeiou". Your if statement will not add one to vowels, then you subscribe to textchanged and you're done. Currently, you aren't looping over the text in the RichTextBox. The Linq approach outlined by fubo works and is much cleaner but going with what you already have what you really should do is:

var characters = rtbDoc.Text.Length;

foreach(var char in rtb.Text.ToLower())
    if(IsVowel(char)) 
         vowel+=1;
Dan Hutt
  • 9
  • 1
  • 3
  • I tried using this 1 since it looked simple enough and quite simple but it did not work out for me unfortunately. But thanks for feedback :) – Cyanic Wolf Apr 15 '16 at 13:46