+ 2

Unable to finish this project please help

The program you are given defines an array with 10 words and takes a letter as input. Write a program to iterate through the array and output words containing the taken letter. If there is no such word, the program should output "No match". Sample Input u Sample Output fun //////////////////////////////// using System; using System.Collections.Generic; using System.Linq; 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(); for (int count = 0; count < 10; count++) { if (words.Contains(letter)) { Console.WriteLine(words[count]); } else { Console.WriteLine("No match") ; } } } } }

12th Oct 2021, 10:56 PM
Rubayet Kamal
Rubayet Kamal - avatar
9 Antworten
+ 3
Hello, you want to call the index of the words array. Here you just call the entire name array. Change the if statement from: if (words.Contains(letter)) to : if(words[count].Contains(letter))
12th Oct 2021, 11:08 PM
Jibraeel Abdelwahhab
Jibraeel Abdelwahhab - avatar
+ 1
Yes, thats because you have an unnessary else statement. You didnt ask that in your question. Just delete else { no match}. To get no match for the overall loop, just add a separate if statement to check at the end.
12th Oct 2021, 11:25 PM
Jibraeel Abdelwahhab
Jibraeel Abdelwahhab - avatar
0
Jibraeel Abdelwahhab Thankfully that gave the output however not exactly how the question asked for. I wanted to get elements only if the condition is met, but it is now showing "No output" along with it.
12th Oct 2021, 11:12 PM
Rubayet Kamal
Rubayet Kamal - avatar
0
Jibraeel Abdelwahhab for example I only wanted the words if the letter matched them and that is it.
12th Oct 2021, 11:15 PM
Rubayet Kamal
Rubayet Kamal - avatar
0
I just tested the code, I'm not sure what you mean. Are you talking about No Match? or is it not giving output at all?
12th Oct 2021, 11:18 PM
Jibraeel Abdelwahhab
Jibraeel Abdelwahhab - avatar
0
For example. For the letter 'u', the expected output is only "fun" but for me it is showing: "No match No match No match No match No match No match No match No match No match Fun"
12th Oct 2021, 11:20 PM
Rubayet Kamal
Rubayet Kamal - avatar
0
Understood what you said but unable to figure out the condition which I should use in the separate if statement.
12th Oct 2021, 11:30 PM
Rubayet Kamal
Rubayet Kamal - avatar
0
Sir I'm not gonna give alll the answers. Its an easy problem. Think about bools.
12th Oct 2021, 11:38 PM
Jibraeel Abdelwahhab
Jibraeel Abdelwahhab - avatar
0
Alright man,Thanks for the helps so far.
12th Oct 2021, 11:38 PM
Rubayet Kamal
Rubayet Kamal - avatar