C-SHARP - 4.2 Exercise: Advanced tables
Hello everyone and welcome to this new video. In this video, we are going to practice on the concept that we saw just before. We saw in the previous video, two-dimensional or multi-dimensional arrays and how to traverse them thanks to nested loops. So I am now going to give you an exercise to put into practice what we have just seen. The exercise is as follows: You will ask the user to enter a word, then the program will look in the table just there to see if the word is present one or more times. So whoever says several times, says that we will have to make a counter. I say no more to you to find how to do it. And at the end of the program, we will display if the word is present The word you typed: (here you must retrieve the word typed by the user) was found n times in the list and here you must retrieve the number of times the word was found. And if the word is not present in the table then we will display: The word you typed is not in the word list So here is the exercise. You will have to create a table using the one I gave you above. Next you will ask the user to enter a number. And your program will look in this table if the word is present 1 2 3 times or not at all. If you block, Don't hesitate to watch the previous video again. Pause the video and we meet just after for the correction. We are back now for the correction of the exercise, I hope you succeeded, otherwise pay attention to the correction. First, you had to create a string array containing 5 rows and 5 columns, i.e. a 2-dimensional array. So we give it the type var then we give it a name liste_mots then = new string brackets, and between the brackets we put the number of rows and columns or 5.5. And to finish we will directly copy and paste this table. Now that we have our table, we had to ask the user to enter a word. We will first do a Console.WriteLine() to prompt the user to enter a word. Then we will interact with it and you remember what function allows to interact with the user? It is the Console.ReadLine() function that we are going to store in a string type variable because the output of this function is a string, we give it the name word_username and we are going to assign it the Console.ReadLine() function. Now I draw your attention to a detail. You see this table, all the words in it start with a lowercase letter. If now the user enters the word monday but with the L in capitals, will our program succeed in testing the equality between the word that the user will enter and the word found in the table? Well no. The compiler will determine that it is not the same word. To avoid this, we will handle this exception by transforming the word that the user will enter into lowercase. During your life as a developer, you have to think of every possible case so that your programs are operational no matter the situation. And to do that in C#, there are several methods that allow you to transform or convert a character string, here I will simply mention 2 of them. But you can consult the Microsoft documentation for all the others. So first there is the ToLower() method. This method allows, as its name indicates, to lower either here to pass all our string in lowercase and to return a copy. And the second, it's called ToUpper() This method, on the other hand, converts all our strings to uppercase and returns a copy. Here I'm talking about returning a copy, I don't know if you remember but when you modify a string, you don't touch the original but you create a copy and you apply the modification or the transformation on it C was a little reminder. So to transform into lowercase what the user will enter after the Console. ReadLine() you put a dot then the ToLower() method; And if the user enters a word in capitals, or if he puts a capital letter at the beginning in the middle, it doesn't matter, we will have a copy of this word returned to lowercase in all cases. We will test for you to see the result. Here I am going to do a Console.WriteLine() and inside the parentheses I am going to put the variable that contains what the user has to enter, therefore password_user and if I run the program. Enter a name: For example, I type House in uppercase enter and you see it is in lowercase. I will do another test, if I raise I go home but this time with the i in capitals I enter and you see? The word is lowercase regardless of the situation the result will be lowercase. We can test the reverse if you want, here instead of transforming into lowercase I will transform into uppercase. I change and I put here ToUpper(). I launch the program, I will write in lower case and you see? We have our word in uppercase. All right, let's put the ToLower() function back and let's move on. We had said in the statement of the exercise that there must be a counter to know how many times the word that the user is going to enter could be present in this table. So we are going to create an int type variable that we are going to call counter and that we are going to initialize to 0. And now we are entering the heart of the program. We need to browse our array. Here first thing, we have a 2-dimensional array so we think directly about what? Nested loops because we will have to traverse the rows as well as the columns. We will directly create two for loops. Either we type for between parentheses, we do an int i = 0; then our condition is what? Here it is the loop that processes the rows, there are 5 so we will write that as long as i is less than the number of rows, i.e. 5 then you loop and we finish by incrementing i each iteration. Then in the braces of the for we will put our second for loop which will allow us to browse each column or each element of a row. Either in the parentheses, we will make an int j = 0 which, like i, will serve as an index. Then the condition, there are 5 elements per line so as long as j is less than 5 and we finish by incrementing j each round. Now that we have our loops to traverse the rows as well as the columns. The goal of our program was to know if the word that the user enters is indeed in the table and how many there are. So where are we going to perform our test to find out if the word is present? C is in our second for loop, Why ? Indeed the first just takes care of going from one line to another while the second for loop takes care of going through each element of a line so it is between the braces of the second for that we are going to perform our test. What are we going to do here? We are going to do an if that if the element of the liste_mots table in the line according to i and in the column according to j is == to the mot_USER then we will increment our counter. I explain, here ja each iteration of the loop will increment by 1 or we will advance in the columns of our first line, and each time this j advances we will retrieve the value located at the location of j and if it is equal to the word entered by the user then we will increment our counter which allows us to know how many times our word is present in this board. And if not, if it is not equal then we advance to the next element until we reach the last of the line. Once at the end of the line, is incremented so we go to the next line and start the loop again to browse the elements of a line. Once we have tested with all the elements of each line then our nested loop stops. And to finish, we said that we had to display whether the word was present or not, if so then we display how many times the word is present in our table. And what allows us to know if it is present in our table? It is the counter So here the condition will be that if counter is strictly greater than 0 roughly that a minimum of 1 is needed then you are going to display the word to us as well as how many times it has been found. Either we do an If between the brackets we put the counter condition strictly greater than 0 and in the braces we will put our Console.WriteLine() Between the brackets we will make a string.Format() quotes and we will write the following sentence: The word you typed: (here we put braces 0 to retrieve the variable that contains the word entered by the user) was found here it is the number of times the word was found nanana times in the list so here we will put the counter so braces 1 after the comma we put the variable word_user comma the counter variable. And to finish I do an else to process if the word is not in this table, here if the word ny is not the condition of the if will never be true and the counter will never increment so we will have the value 0 and we will enter the else because to enter the if the counter must be strictly greater than 0 be at least 1 And in the braces, we will make a Console.WriteLine allowing to display the following sentence The word you typed is not in the word list We will now test if everything works correctly. We launch the program. Enter a name: I will type day which is several times in our table, I enter and it displays The word you typed: day was found twice in the list. If we look in our table, we have 2 times a day I will test with another word this time in capitals I will take Monday. I restart the program enter a name I type the first 3 letters of Monday in capital letters j enter and you see ? he found it well despite that I typed the first 3 letters in capital letters thanks to the ToLower that we put here. I am going to remove it so that you can see I restart the program I do the same thing again and this time you see, it does not find it because Monday in lower case and Monday in upper case are different. I'm going to hand over the ToLower method for a final test. Finally, I will test with a word that is not in the table, I launch the program and I'm going to type house I enter and you see? We have a good poster The word you typed is not in the word list So our program handles well the case where the word is not in the table. So don't hesitate to redo the exercise if you haven't succeeded. In any case, I hope you liked this video and that everything was clear to you. See you next time in a new video.