+ 2

ماالخطأ في كودي لهذه المسألة؟

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".

19th Jul 2022, 8:10 AM
Zina Shekh Alzoor
Zina Shekh Alzoor - avatar
9 Answers
+ 3
Zina Shekh Alzoor 1.Remove counter "count" as you use foreach loop so no need for counter 2."WriteLine" L is Capital so replace it 3. there is semicolon ; at the end of string []words ...
19th Jul 2022, 8:30 AM
Aly Alsayed
Aly Alsayed - avatar
+ 3
Zina Shekh Alzoor Print matched value and increment count Outside the loop print "No match" if count is 0 count == words.Length will give "no output" message because count would not be equal to words.Length untill all value doesn't match string letter = Console.ReadLine(); int count = 0; //your code goes here foreach (string k in words) { if (k.Contains(letter)) { Console.WriteLine(k); count++; } } if (count == 0) Console.WriteLine("No match");
19th Jul 2022, 9:06 AM
A͢J
A͢J - avatar
+ 1
int count++; is invalid. Just put count++; in else part. After the loop, If count is equal to array words length then output "No match" It's means never if part executed..
19th Jul 2022, 8:35 AM
Jayakrishna 🇮🇳
+ 1
string letter = Console.ReadLine(); int count =0; //your code goes here foreach (string k in words ) { if (k.Contains(letter)) { Console.WriteLine(k); } else count ++; } if (count==words.length) { Console.WriteLine("No match"); }
19th Jul 2022, 8:47 AM
Jayakrishna 🇮🇳
+ 1
using System; namespace SoloLearn { class Program { static void Main(string[] args) { string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" }; string letter = Console.ReadLine(); //your code goes here foreach (string k in words ) { if (k.Contains(letter)) Console.WriteLine(k); else Console.WriteLine("No match"); } } }}
19th Jul 2022, 8:51 AM
Aly Alsayed
Aly Alsayed - avatar
+ 1
Thanks👌
19th Jul 2022, 9:11 AM
Zina Shekh Alzoor
Zina Shekh Alzoor - avatar
0
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 (string k in words ) { if (k.Contains(letter)) Console.Writeline(k); else Console.Writeline("No match"); } int count ++ } }
19th Jul 2022, 8:11 AM
Zina Shekh Alzoor
Zina Shekh Alzoor - avatar
0
string letter = Console.ReadLine(); int count =0; //your code goes here foreach (string k in words ) { if (k.Contains(letter)) { Console.WriteLine(k); count ++; } هكذا؟ else if (count==words.length) { Console.WriteLine("No match");}
19th Jul 2022, 8:42 AM
Zina Shekh Alzoor
Zina Shekh Alzoor - avatar
0
Yes. This AJ solution is cool.. I followed your approach.
19th Jul 2022, 9:21 AM
Jayakrishna 🇮🇳