C-SHARP - 4.2 Exercise: Advanced tables

This exercise lets us practise 2D arrays and nested loops. Starting from a pre-defined 5x5 string array of words, your program must ask the user to enter a word, search the whole array for it and tell the user whether the word was found and how many times. If the word does not appear at all, display a message saying so. To make the program user-friendly, accept upper-case input by converting the entry to lower-case before comparing.

Build the 2D array, prompt the user with Console.WriteLine, read the answer with Console.ReadLine() and chain .ToLower() to neutralise the case. Initialise an int compteur = 0; to count occurrences. Then nest two for loops: the outer one iterates over the rows, the inner one over the columns. Inside the inner loop, compare each cell with the user input and increment the counter when they match.

string[,] mots = new string[5, 5]
{
    /* 5 rows of 5 words */
};

Console.WriteLine("Enter a word:");
string motUser = Console.ReadLine().ToLower();
int compteur = 0;

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        if (mots[i, j] == motUser)
        {
            compteur++;
        }
    }
}

if (compteur > 0)
{
    Console.WriteLine(string.Format("The word {0} was found {1} time(s) in the list.", motUser, compteur));
}
else
{
    Console.WriteLine("The word is not in the list.");
}

What we practised

  • Build and query a 2D string array.
  • Use nested for loops to visit every cell.
  • Normalise user input with ToLower() for robust comparisons.
  • Maintain a counter and display the right message at the end.

Test with a word present multiple times (such as jour) and you will see the counter reach the right number. Test with a fully upper-case word: thanks to ToLower() the match still works. Without that call, the comparison would fail. Try a word that is not in the list to confirm the else branch fires. As a developer, always anticipate input variations so the program stays robust whatever the user types.

Summary

This lesson provides a hands-on exercise on 2D arrays in C#, where students learn to search for a user-entered word within a 5x5 string array and count its occurrences. The correction demonstrates key concepts including array initialization, user input handling, and case-insensitive string comparison using the `.ToLower()` method.

Key points

  • Create a 2D string array using the syntax: new string[rows, columns]
  • Implement nested loops to traverse multidimensional arrays efficiently
  • Use a counter variable to track the number of times a word appears in the array
  • Apply case-insensitive comparison by converting user input to lowercase with `.ToLower()`
  • Handle both found and not-found scenarios with appropriate console output
  • Use `Console.ReadLine()` for user interaction and `Console.WriteLine()` for display

FAQ

Why do we use .ToLower() when searching for words in the array?

The `.ToLower()` method ensures case-insensitive comparison. Since the array contains lowercase words, converting user input to lowercase prevents mismatches (e.g., 'MAISON' vs 'maison') and makes the program more robust for all user inputs.

What is the purpose of the counter variable in this exercise?

The counter variable tracks how many times the searched word appears in the 2D array. It increments within the nested loops each time a match is found, allowing the program to report the total occurrences to the user.

How do you properly declare and initialize a 2D array in C#?

Use the syntax: `var arrayName = new string[rows, columns];` for example, `new string[5, 5]` creates a 5x5 array. You can then populate it with string values or use direct initialization with curly braces and commas separating rows.