1

What I'm trying to do is fairly simple. The user inputs a string. The program checks and counts the number of vowels present in that text string.

    char[] vowels = {'a', 'e', 'i', 'o', 'u'};
    int counter = 0;

    string s = txtInput.Text.Trim();
    char[] arr = s.ToCharArray();


    foreach (char i in arr)
    {
        //checks the 2 arrays for matches
        counter++;
    }

How can I check the 2 arrays to see if there are any matches?

Thank you.

9 Answers 9

6
arr.Any(p => vowels.Contains(p));

Update: To calculate number of matches you may use;

 int count = arr.Where(p => vowels.Contains(p)).Count();
Sign up to request clarification or add additional context in comments.

2 Comments

In the question count is not asked, but you may find using int count = arr.Where(p => vowels.Contains(p)).Count();
The question asks for count: The program checks and counts the number of vowels
4

You can use Array.IndexOf() for this:

if (Array.IndexOf(vowels, i) >= 0)
{
    // the character is present
}

Comments

2

Simply use two foreach loops

foreach(var i in arr)
    {
        foreach(var j in vowels)
        {
            if(i==j)
            {
                counter++;
            }
        }
    }

Comments

1

I would use LINQ:

arr.Select(c => vowels.Contains(c));

In order to get the count of characters you can use Count method instead of Select. If you want to just know if there are any vowels, then use Any - it will terminate as soon as it founds the first match.

1 Comment

I have no knowledge in LINQ as of now since I'm merely in the beginner stage if learning C#. But thanks for that information. I'll keep that in mind. It seems easier.
1

Try this:

 if (vowels.Contains(i))
 {
    counter++;
 }

Comments

0

What you could do is to create a list of characters for vowels, and then use the .contains method.

Comments

0

You can use LINQ for that!

bool anyMatches = arr.Intersect(vowels).Any();

or, if you want the matches:

var matches = arr.Intersect(vowels).ToList();

EDIT:

or to count the exact number of matches:

int count = arr.Count(x => vowels.Contains(x));

2 Comments

This counts the number of different vowels, not the total number of vowels ("woot" produces one 'o')
Yes, look at the presented code - for each character in arr he wants to check the other array for matches.
0

Use this Linq Query

var q=from c in s 
  where vowels.Contains(c)
  group c by c into g
  select  new {vowel=g.Key, count=g.Count()};

then you can iterate and do what you like with the contents

e.g.

 foreach (var x in q) {
     Console.WriteLine(String.Format("{0} : {1}",x.vowel,x;count));
 }

Comments

0
Hash<char> vowels = //initialize to vowels.

foreach(char c in input)
{
   if(hashset(vowels) contains the character)
      increment counter.
}

return counter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.