0

Okay i might not have explained it to the best of my ability but i'm a beginner and i would like to make a piece of code that does this : you have a string and you need to find each vowel in it and multiply each vowel's position in the string by its position in the alphabet and add all the sums together example : steve: has 2 vowels the first e's position is 3 and its position in the alphabet is 5. and the second's position in the alphabet and the string is 5 so the sum is 5*3 + 5*5 = 40 this is what i did . idk what to do now or how to approach it

 var vowels = new char[] {'a', 'e', 'i', 'o', 'u', 'y', 'A','E','I', 'O', 'U','Y'};
        var chars = new List<char>();
        List<int> indexes = new List<int>();

        Console.WriteLine("Write something : ");
        var input =  Console.ReadLine();

        int index;
        foreach (var vowel in vowels)
        {
            if (input.Contains(vowel))
            {
                index = input.IndexOf(vowel);
                indexes.Add(index + 1);
                chars.Add(vowel);
            }

        }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
crystals
  • 45
  • 7

3 Answers3

0

Consider this approach:

using System;
using System.Linq;
using System.Collections.Generic;

namespace Whatever
{
    class Program
    {
        static void Main(string[] args)
        {
            var vowels = new Dictionary<string, int>(5, StringComparer.OrdinalIgnoreCase) { { "a", 1 }, { "e", 5 }, { "i", 9 }, { "o", 15 }, { "u", 21 } };

            Console.WriteLine("Write something : ");
            var input = Console.ReadLine();

            var sum = input.Select((value, index) => new { value, index })
                .Sum(x =>
                    {
                        vowels.TryGetValue(x.value.ToString(), out var multiplier);
                        return (x.index + 1) * multiplier;
                    });

            Console.ReadLine();
        }
    }
}

The Select projects the original string as an anonymous type with the char and its index included.

The Sum checks if the string is a vowel - and if it is it multiplies the position (index + 1) by the position in the alphabet (from vowels).

vowels is case insensitive so that "A" and "a" are treated the same.

If the compiler complains about the out var then use:

int multiplier = 0;
vowels.TryGetValue(x.value.ToString(), out multiplier);
return (x.index + 1) * multiplier;

instead.

mjwills
  • 23,389
  • 6
  • 40
  • 63
0

i figured it out right here

for (int i = 0; i < indexes.Count; i++)
        {
            sumofone += indexes[i] * (char.ToUpper(chars[i]) - 64);
        }
crystals
  • 45
  • 7
  • Consider doing this logic **inside** of your `if` statement to avoid the need for `indexes` and `chars`. Similar to @Gauravsa's approach. – mjwills Sep 03 '18 at 00:38
-1

You can do this (Reference is from here):

    var vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };

    Console.WriteLine("Write something : ");
    var input = Console.ReadLine().ToLower();

    int total = 0;
    for (int temp = 1; temp <= input.Length; temp++)
    {
        if (vowels.Contains(input[temp - 1]))
        {
            total += temp * (char.ToUpper(input[temp -1]) - 64);
        }
     }

     Console.WriteLine("The length is " + total);
Gauravsa
  • 6,330
  • 2
  • 21
  • 30