user3596292
user3596292

Reputation: 15

Converting letters to dashes

I have been asked to make a game at work where I have to change the letters to dashes. I have been told to use a multi dimensional array but I get the full word out of a text file. This is the code i have so far

Console.WriteLine("Enter file path:");
            string filePath= Console.ReadLine();

            // read all the lines from the file
            string[] lines = File.ReadAllLines(readFilePath);

            // get a random number between 0 and less than the number of lines in the file
            Random rand = new Random();
            int chosenLineIndex = rand.Next(lines.Length);

            // choose the line at the line number
            string chosenLine = lines[chosenLineIndex];

            // write the line to the Console
            Console.WriteLine(chosenLine);

            // make an array containing the only the chosen line
            string[] chosenLines = new string[] { chosenLine };

So does anyone know how to write the letters separately to the array instead of the full word?

Upvotes: 0

Views: 135

Answers (2)

Kevin D
Kevin D

Reputation: 485

I believe you are looking for String.ToCharArray

Upvotes: 3

Selman Genç
Selman Genç

Reputation: 101701

This will give you an array that contains each character separately as a string.

string[] chosenLines =  chosenLine.Select(x => x.ToString()).ToArray();

You can also use a char[] array instead.Then you won't need Select, you can just use String.ToCharArray method.

Upvotes: 1

Related Questions