I am attempting to solve a problem. There is a bunch of gibberish, but after every vowel is a letter that is used to piece together a legible word. My code always returns either
System.Char[]
System.Char[]
System.Char[]
or
System.Collections.Generic.List`1[System.Char]
System.Collections.Generic.List`1[System.Char]
System.Collections.Generic.List`1[System.Char]
for some random reason. I know it’s a simple problem but I’m having so much trouble with it.
Here is my code currently:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static string Decypher(string encryptedMessage)
{
List<char> letters = new List<char>();
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (int i = 0; i <= encryptedMessage.Count() - 1; i++)
{
if (vowels.Contains(encryptedMessage[i]))
{
letters.Add(encryptedMessage[i + 1]);
i++;
}
}
return letters.ToString();
}
public static void Main(string[] args)
{
string testCase1 = "fksahnlgueyilfhnalfkjnhdssaokjfhndsfiwaourhnfdjgbalfkjshedfnsf";
string testCase2 = "mkjmnacioudhrieeqwthyiugueresjfgwatfhwghfnhgnffn";
string testCase3 = "elruoqywicwnjksakvfbsgyohuehnghiefhggadfgsfsfs";
Console.WriteLine(Decypher(testCase1));
Console.WriteLine(Decypher(testCase2));
Console.WriteLine(Decypher(testCase3));
}
}
>Solution :
Some types have a custom ToString method but most just inherit the default implementation from the Object class, which is to return the name of the type. That’s exactly what you’re seeing from this:
return letters.ToString();
If what you actually want is a string containing the characters from letters then the most obvious option is to use the string constructor that takes a char array as an argument:
return new string(letters.ToArray());