-2

I'm writing a code which counts the most frequent word in an array of string , I'm also trying to count the number of vowels in each line of the word , in other words the total number of vowels in all the words which are in the array , I've written the code but it's not working and I can't figure out what's the problem with it , is it because I'm comparing string with char ?

using System;
using System.Collections.Generic;

namespace lab1._2
{
class Program
{
    static String findWord(String[] arr)
    {
        Dictionary<String, int> hs =
            new Dictionary<String, int>();
        for (int i = 0; i < arr.Length; i++)
        {
            if (hs.ContainsKey(arr[i]))
            {
                hs[arr[i]] = hs[arr[i]] + 1;
            }
            else
            {
                hs.Add(arr[i], 1);
            }
        }
        String key = "";
        int value = 0;

        foreach (KeyValuePair<String, int> me in hs)
        {
            if (me.Value > value)
            {
                value = me.Value;
                key = me.Key;
            }
        }
        return key;
    }
    static void Main(string[] args)
    {
        int s;
        //char[] v = new char[10] {'a','A','e','E','i','I','o','O','u','U'};
        Console.WriteLine("Enter size of array : ");
        s = Convert.ToInt32(Console.ReadLine());
        string[] arr = new string[s];
        Console.WriteLine("Enter string elements : ");
        for (int i = 0; i < s; i++)
        {
            arr[i] = Console.ReadLine();
        }
        Console.WriteLine("\nArray elements : ");
        for (int i = 0; i < s; i++)
        {
            Console.WriteLine(arr[i]);
        }
        Console.WriteLine("\nThe most frequent word : ");
        Console.WriteLine(findWord(arr));
        int vowel = 0, cons = 0;
        for (int i = 0; i < arr.Length; i++)
        {
                if (arr[i] == "a" || arr[i] == "e" || arr[i] == "i" || arr[i] == "o" || arr[i] 
        == "u")
                {
                    vowel++;
                }
                else
                    cons++;
        }
        Console.WriteLine("Vowels : ");
        Console.WriteLine(vowel);
        Console.WriteLine("Consants : ");
        Console.WriteLine(cons);
        }
    }
}
navnath
  • 3,548
  • 1
  • 11
  • 19
  • 2
    Be more specific on how it is not working – DJ Burb Sep 13 '21 at 19:16
  • First step. Divide the problem into smaller problems. Try to solve those smaller problems and ask more specific questions if you don't know how to proceed. – Leron Sep 13 '21 at 19:24
  • In this line if (arr[i] == "a" || arr[i] == "e" || arr[i] == "i" || arr[i] == "o" || arr[i] == "u") you compare an string with a char. arr is an Array of strings and arr[i] returns a string. You need to check how often the vocal is in arr[i] and not if arr[i] is a vocal. – Andy Sep 13 '21 at 19:26
  • @DJBurb it's not counting the vowels and the constants are just couting the number of words in the array , basically the size of array – Alex Alecco Sep 13 '21 at 19:34
  • Use https://dotnetfiddle.net/ to test each part of your code. Also, take a look at https://stackoverflow.com/questions/18109890/c-sharp-count-vowels – eglease Sep 13 '21 at 20:31

1 Answers1

1

I think the problem is that arr is an array of string and you are iterating through it as if it was a single string.

A simple way of doing this would be to have a nested loop.

    foreach (var s in arr) // s is a string
    {
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
            {
                vowel++;
            }
            else
                cons++;
        }
    }

https://dotnetfiddle.net/tI3oTc

using System;

public class Program
{
    public static void Main()
    {
        int vowel = 0;
        int cons = 0;
        string[] arr = new string[]{"test"};
        foreach (var s in arr) // s is a string
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
                {
                    vowel++;
                }
                else
                    cons++;
            }
        }

        Console.WriteLine(vowel);
    }
}
eglease
  • 2,445
  • 11
  • 18
  • 28
  • Note that there are much better ways of doing this but it should work. – eglease Sep 13 '21 at 20:38
  • `foreach` can be replaced with a regular `for` loop if you want. – eglease Sep 13 '21 at 20:39
  • Also, for characters use `'a'` instead of `"a"` which is a string. – eglease Sep 13 '21 at 20:41
  • Your code example won't work with spaces and punctuation marks. They will be counted as consonants. – eglease Sep 13 '21 at 20:42
  • 1
    I've tried your snippet but it's displaying this error , Error CS0136 A local or parameter named 's' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter , any clue how to fix this ? – Alex Alecco Sep 14 '21 at 21:22
  • do you have any other variables named `s`? Try renaming it to `myStr` or something like that. – eglease Sep 14 '21 at 21:24
  • https://dotnetfiddle.net/tI3oTc works fine for me. Also, the logic will not pick up capital letters. To detect them, it is easier just to convert everything to lower case or capital and work from that. – eglease Sep 14 '21 at 21:30