Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 296361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:32:31+00:00 2026-05-12T06:32:31+00:00

I tried to design a program which counts the vowels in a sentence. In

  • 0

I tried to design a program which counts the vowels in a sentence.

In my code, I used a foreach statement with the if/else if statement. I would like to convert these line of code using the switch statement
but I’m not really sure where to go. Do I need to add a new method? I would appreciate your help.

This is what I tried so far: I checked this one is very wrong. The case 1 for example needs to have a constant. I’m not sure what constant shall I use here.

foreach (char v in yourSentence)
{
    switch (v)
    {
    case 1: 
        (v==ch1); 
        counta++; 
        j++; 
        break;

    case 2: 
        (v==ch2); 
        counte++; 
        j++; 
        break;
    case 3: 
        (v==ch3); 
        counti++; 
        j++; 
        break;
    case 4: 
        (v==ch4); 
        counto++; 
        j++; 
        break;
    case 5: 
        (v==ch3); 
        counti++; 
        j++; 
        break;             
    }
}

Another question: I tried to change the color of the display text in the listBox. Is that possible to have different colors? What I also tried here is the first 5 (listBox1.Items.Add) are violet. And the sum of the (listBox1.Items.Add) is blue. But it seems that it didn’t change. Did I miss something here?

private void btnCount_Click(object sender, EventArgs e)
{
    string yourSentence;
    yourSentence = textBoxVowels.Text.ToLower().Trim();

    char ch1 = 'a';
    char ch2 = 'e';
    char ch3 = 'i';
    char ch4 = 'o';
    char ch5 = 'u';

    int counta = 0;
    int counte = 0;
    int counti = 0;
    int counto = 0;
    int countu = 0;

    int j = counta + counte + counti + counto + countu;



    foreach (char v in yourSentence)
    {
        if (v == ch1) { counta++; j++; }

        else if (v == ch2) { counte++; j++; }

        else if (v == ch3) { counti++; j++; }

        else if (v == ch4) { counto++; j++; }

        else if (v == ch5) { countu++; j++; }
    }

    listBox1.Items.Add("There are " + counta.ToString().Trim() + " a's in the sentence");
    listBox1.Items.Add("There are " + counte.ToString().Trim() + " e's in the sentence");
    listBox1.Items.Add("There are " + counti.ToString().Trim() + " i's in the sentence");
    listBox1.Items.Add("There are " + counto.ToString().Trim() + " o's in the sentence");
    listBox1.Items.Add("There are " + countu.ToString().Trim() + " u's in the sentence");
    listBox1.Font = new Font("Arial", 12, FontStyle.Bold);
    listBox1.ForeColor = Color.Violet;

    listBox1.Items.Add("There are " + j.ToString().Trim() + " vowels in the sentence");
    listBox1.ForeColor = Color.Blue;
}


private void btnClear_Click(object sender, EventArgs e)
{
    textBoxVowels.Text = null;
    listBox1.Items.Clear();
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:32:31+00:00Added an answer on May 12, 2026 at 6:32 am

    I think you misunderstood how ‘switch/case’ works. Just change them to this:

    case 'a': 
         counta++; 
         j++; 
         break;
    

    Case needs a constant value that should be compared to the current character.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
I tried to package a Twisted program with py2exe, but once I run the
I tried the following code in LINQPad and got the results given below: List<string>
I'm trying to design a code where one guess a number. I defined the
I'm taking an operating systems design class in which they have given us a
I tried to create a ValueType . I understand that creating a struct would
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
I tried to install Delphi 7 on Vista several times and Vista prevented me
I tried scouring the web for help on this issue, but there are so
I tried recently to use NAnt (beta 0.86.2962.0) to run some unit tests compiled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.