12345678i9
12345678i9

Reputation: 21

Is there a way for me to store the individual letters of a string in a char array?

I am trying to recreate wordle on C#. After I get the user input, I am trying to store each individual letters of the string into a 2D char array. Below is my code.

        // Store words            
        char[,] userWords = new char[6, 5];

        // Ask user for word
        Console.Write("\nGuess a word!: ");

        // Get user words
        for (int i = 0; i < userWords.GetLength(0); i++)
        {
            for (int j = 0; j < userWords.GetLength(1); j++)
            {
                userWords[i, j] = Convert.ToChar(Console.ReadLine());
                
                // Test 
                Console.WriteLine(userWords[i, j]);
                
            }
        }

I am assuming that each word I type is being stored in one section of the array, which is why it isn't working. How can I store each letter of each word in the array?

Upvotes: 0

Views: 275

Answers (3)

Pete
Pete

Reputation: 116

Well, you can't have the Console.ReadLine() inside the inner for loop as that would require the user to input a word after every iteration of reading the word’s individual letters.

The program now asks for user input after each iteration of "i" so that you can fill the 2D array with 6 words, each 5 letters long.

// Store words            
char[,] userWords = new char[6, 5];

// Get CHARS from user's word
for (int i = 0; i < userWords.GetLength(0); i++)
{
    // Ask user for word
    Console.Write("\nGuess a word!: ");

    // Temp store for user's word
    string wordTemp = Console.ReadLine();
    
    for (int j = 0; j < userWords.GetLength(1); j++)
    {
        userWords[i, j] = wordTemp[j];

        // Test 
        Console.WriteLine(userWords[i, j]);
    }
}

Upvotes: 1

Rufus L
Rufus L

Reputation: 37080

I think the problem is you're trying to convert a string to a character. Instead, you can use Console.ReadKey().KeyChar to capture a single character from the user.

Here's a brief example, I'll probably update this tomorrow with a more thoughtful answer, though:

// Store words. First dimension is rows, second is columns        
char[,] userWords = new char[6, 5];

// Ask user for word
Console.Write("\nGuess a word: ");

// Get user words
for (int row = 0; row < userWords.GetLength(0); row++)
{
    for (int col = 0; col < userWords.GetLength(1); col++)
    {
        userWords[row, col] = Console.ReadKey().KeyChar;
    }

    // See if they guessed the correct word here
}

Upvotes: 0

John Wu
John Wu

Reputation: 52280

Not completely sure but I'm guessing you want something more like this:

for (int i = 0; i < userWords.GetLength(0); i++)
{
    var word = Console.Readline():
    for (int j=0; j< Math.Min(word.Length, userWords.GetLength(1)); j++)
    {
        userWords[i,j] = word[j];
    }
}

Reason: Console.ReadLine() return a string, which you can treat as a one-dimensional array of characters by using the indexer property.

Upvotes: 0

Related Questions