+ 2

Help on "Words" Problem.

So I did the code, but shows no output. No idea where did i go wrong. Please help. 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; //your code goes here foreach (var i in words) { if(words[i].Contains(letter)) { Console.WriteLine(words[i]); count++; } } if (count == 0) { Console.Write("No match"); } } } }

5th Jan 2021, 2:15 AM
Faris Mohamed
Faris Mohamed - avatar
7 ответов
+ 4
Faris Mohamed Here var i, is of type String and you're doing words[i] , treating i as an int where i is actually a String. Write, i.Contains(letter) . . Console.WriteLine(i)
5th Jan 2021, 2:25 AM
Minho
Minho - avatar
+ 6
Try this . 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(); string mensaje = ""; for(int i = 0; i < words.Length;i ++) { if(words[i].Contains(letter)) { mensaje += words[i] + "\n\r" ; } } if (mensaje == "") { mensaje = "No match"; } Console.Write(mensaje); } } }
6th Jan 2021, 10:43 PM
Monica Martinez
Monica Martinez - avatar
+ 4
Hi guys! Here is my solution using foreach! 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; foreach(string w in words) { if (w.Contains(letter)) { Console.WriteLine(w); count++; } } if(count==0) { Console.WriteLine("No match"); } } } }
13th Jul 2021, 9:21 AM
jose ant gutierrez
jose ant gutierrez - avatar
+ 3
You're welcome 😊
5th Jan 2021, 2:38 AM
Minho
Minho - avatar
+ 1
man, how can i not notice that! thank you so much for identifying!.
5th Jan 2021, 2:36 AM
Faris Mohamed
Faris Mohamed - avatar
0
Words 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
5th Jan 2021, 2:18 AM
Faris Mohamed
Faris Mohamed - avatar
0
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; //your code goes here for(int i=0;i<words.Length;i++){ if(words[i].Contains(letter)){ Console.WriteLine(words[i]); count++; } } if(count==0){ Console.WriteLine("No match"); } } } }
6th Sep 2022, 7:52 PM
mahmoud el hefny