+ 2
Help me solve!
You are given a program that takes an array of 10 words and takes a letter as input. Write a program to iterate over an array and output words containing the letter received. If there are no matching words, the program should print "No match". Sample Input Data u Sample Output Data fun my attempt to solve the problem : https://code.sololearn.com/cAWH4n3SwyVJ/?ref=app But it's doesn't work đ
6 Answers
+ 1
Mr.Mr
You declared count as int
Then converted to String
Checking count in letter
Incrementing count.
If count is converted to String then how you can increment it?
learn again:
1 - data types and their uses
2 - foreach loop working behaviour
+ 2
Mr.Mr
Count is to check how many word has matching letter so if there are 2 or more than 2 matching words then count will be increase accordingly. So on the basis of count you can print "No match" if count is 0
+ 1
Mr.Mr
You have to print matched value not count.
Print matched value and increase count.
Outside the loop check if count is 0 then print "No Match"
+ 1
Mr.Mr
You have to check letter in each word of the given array. If match found then print word.
Here is your solution. Check and find your mistakes:
string letter = Console.ReadLine();
int count = 0;
// count = Convert.ToString();
foreach(string str in words) {
if(str.Contains(letter)) {
Console.WriteLine(str);
count++;
}
}
if (count == 0) {
Console.WriteLine("No Match");
}
0
Understand
0
I did not understand why "count" is here, so I inserted it into the foreach loop