Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem with C# Return After Converting From List to Array

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading