0

C#.I need your help!

In this program if string letter exists in the array words it displays the word containing the letter. How can I make the program to output "No match" one time if the letter is not exist in the all array? using System; using System.Collections.Generic; namespace Code_Coach_Challenge { class Program { static void Main(string[] args) { string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" }; string letter = Console.ReadLine(); int count = 0; for(count=0; count<10; count++) { if(words[count].Contains(letter)) Console.WriteLine(words[count]); } } } //your code goes here }

28th May 2022, 6:14 AM
Vafidis Vaios
Vafidis Vaios - avatar
4 Réponses
+ 3
Well you can have a boolean variable which is set to false by default . And you can set it to true in the if statement in the for loop ( if your program finds the word the boolean will be set to true ) . You can check if the boolean is false with an if statement OUTSIDE the loop and if it was false you can output no match. here is how i did it https://code.sololearn.com/cOlK6R7G4Fi0/?ref=app
28th May 2022, 4:37 PM
Ali
+ 2
You shouldn't be using and modifying variable <count> in the for...loop. You should instead create and use another variable dedicated as the loop counter. The only time when you should modify (increment) <count> is when you found an array <words> element that contains <letter>. Once you're finished with the loop, you go use `if` conditional to verify whether <count> value was zero which indicates no match was found And when <count> was actually zero, you go print that "No match" on the screen.
28th May 2022, 7:05 AM
Ipang
+ 1
Thanks a lot...I finally managed to solve it, with your help!!!
28th May 2022, 7:05 PM
Vafidis Vaios
Vafidis Vaios - avatar
0
I tried the ! Operator but it displays the message "No match" for every index not including the letter. I just want to do this only one time if the letter is not included in the 10 indexes!
28th May 2022, 6:18 AM
Vafidis Vaios
Vafidis Vaios - avatar