0

C#Help to understand the solution to the problem.

he 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 My code: using System; using System.Linq; namespace Задача_Слова_50хп { 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; //code here foreach (string n in words) { if (words.Contains (letter)) { Console.WriteLine(words); } else { Console.WriteLine("No match"); } } } } }

23rd Apr 2021, 12:46 PM
Артем Попов
Артем Попов - avatar
10 Antworten
+ 2
if (words.Contains (letter)) Should try this: if (n.Contains (letter))
23rd Apr 2021, 1:05 PM
Jerry Hobby
Jerry Hobby - avatar
+ 1
Next line. Console.WriteLine(n)
23rd Apr 2021, 1:37 PM
Jerry Hobby
Jerry Hobby - avatar
+ 1
Outstanding. That if statement wasn’t in the original code. That’s why I didn’t see it. Glad you got it fixe.
24th Apr 2021, 4:07 PM
Jerry Hobby
Jerry Hobby - avatar
0
Thnx. Works better, but still not true.
23rd Apr 2021, 1:10 PM
Артем Попов
Артем Попов - avatar
0
Thnx a lot. But right answer must be f.e.: "n" out: fun etc. Now result is: No match No match No match No match football No match No match learn No match No match
24th Apr 2021, 3:15 PM
Артем Попов
Артем Попов - avatar
0
I think need to use int count = 0. But how to use I don't know.
24th Apr 2021, 3:18 PM
Артем Попов
Артем Попов - avatar
0
With your code that you provided, you need to change it like below. What you are doing in this code is: For n in words —- That means “n” will be one of the words in the list. For the body of the loop, you want to examine one word, not all of them. So instead of using “words”, you use “n”. Then each iteration will use the next word from words as n. Notice all I changed was a couple of uses of “words” to “n”. The logic was fine. You only need to search a word, not the whole list. foreach (string n in words) { if (n.Contains (letter)) { Console.WriteLine(n); } else { Console.WriteLine("No match"); } }
24th Apr 2021, 3:30 PM
Jerry Hobby
Jerry Hobby - avatar
0
Yes, I tried to run the program with this code, but in the output I got the wrong result. I found a solution. foreach (string n in words) { if (n.Contains(letter)) { Console.WriteLine(n); count++; } } while (count == 0) { Console.WriteLine("No match"); }
24th Apr 2021, 3:57 PM
Артем Попов
Артем Попов - avatar
0
As you can see, I used "count ++" and then after the "foreach" block I entered the "while" block with a condition. The clue for the correct solution of the problem was just in "count = 0". Thank you for your responses.
24th Apr 2021, 4:00 PM
Артем Попов
Артем Попов - avatar
0
I'am so sorry, I put a mistake. Not "while" need to use "if".
24th Apr 2021, 4:04 PM
Артем Попов
Артем Попов - avatar