0
Words - C#. Need 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 I used a loop and the Contains() keyword in an IF statement but I'm confused from that point.
23 Réponses
+ 27
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 num = 0;
for( int count = 0; count < 10; count++){
if(words[count].Contains(letter)){
Console.WriteLine(words[count]);
num++;
}
}
if(num == 0){
Console.WriteLine("No match");
}
}
}
}
+ 5
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 (string i in words){
if ( i.Contains(letter)){
Console.WriteLine(i);
count++;
}
}
if ( count == 0){
Console.WriteLine("No match");
}
}
}
}
+ 4
I changed your code،
Use lowercase letters.
https://code.sololearn.com/c6PzeVWHMJKE/?ref=app
+ 3
For this question, you should ask for clarifications on specifications before you start.
Explicitly, when there are more than one words fulfilling the criteria, should the words be displayed in same line or in separate lines.
If they should be displayed in separate lines, you just Writeln the word when it is a match.
If they should be displayed in one line, you use a template string to store each matched word in each iteration, and add a trailing space; and after the for loop, print a slice of the template string, without the last space.
+ 3
int count = 0;
bool exist=false;
//your code goes here
foreach(var item in words)
{
if(item.Contains( letter))
{
Console.WriteLine(item);
exist=true;
}
}
if (exist==false)
{
Console.WriteLine("No match");
}
+ 2
MrDevEzeoke , show your attempt so somebody can help you.
+ 2
MrDevEzeoke , you can try to solve it this way: first don't use count variable in the loop initialization (you can call it "i" for example). Count is expected to be incremented if there is a match. Use it in the first if clause - > if (words[i].Contains(letter)) - > inside the body count++, then print the match. Outside the loop check with if condition whether count is 0 (that should mean that there is no match) and print the necessary message.
+ 1
string[] words = {
"home",
"programming",
"victory",
"C#",
"football",
"sport",
"book",
"learn",
"dream",
"fun"
};
string letter = Console.ReadLine();
int count = 0;
foreach(string i in words)
{
if (i.Contains(letter))
{
Console.WriteLine(i);
count += 1;
}
}
if (count == 0)
{
Console.WriteLine("No match found");
}
0
int count = 0;
for(count = 0; count < 10; count++)
{
Console.WriteLine(count);
if(letter.Contains(words ))
{
}
else
{
Console.WriteLine("No match");
}
I did not add anything in the IF statement where I am confused.
0
MrDevEzeoke , please link your code to the Playground.
0
I do not know how with a project. TheWh¡teCat 🇧🇬 could you tell me how?
0
MrDevEzeoke , choose code bits from your profile - > "+" button - > choose a language - > C#. Paste your code there. Save your code. Paste the link here in your question.
0
https://code.sololearn.com/caxUpqWQSZQk/?ref=app
Sorry, it took long.
0
Gordon Thank you I understand now. TheWh¡teCat 🇧🇬 I did that Thank you.
0
Words program need 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".
I did the program in order to output the word that contains letter, but also it outputs the sentence "No match".
How can I fix it in order to output only what is needed?
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 a in words){
if(a.Contains(letter)){
Console.WriteLine(a.Substring(0));
}
}
Console.WriteLine("No match");
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;
bool exist=false;
//your code goes here
foreach(var item in words)
{
if(item.Contains( letter))
{
Console.WriteLine(item);
exist=true;
}
}
if (exist==false)
{
Console.WriteLine("No match");
}
}
}
}
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
foreach(var word in words)
{
if (word.Contains(letter))
{
Console.WriteLine(word);
}
}
while (true)
{
if (words[count].Contains(letter))
{
break;
}
else count++;
if (count == 10) { Console.WriteLine("No match");
break;
}
}
Console.ReadKey();
}
}
}
0
foreach(var word in words)
{
if (word.Contains(letter))
{
Console.WriteLine(word);
}
}
while (true)
{
if (words[count].Contains(letter))
{
break;
}
else count++;
if (count == 10) { Console.WriteLine("No match");
break;
}
}
Console.ReadKey();
}
}
}
2nd August 2021, 9:40 PM
رسول
0
int count = 0;
for(count = 0; count < 10; count++)
{
Console.WriteLine(count);
if(letter.Contains(words ))
{
}
else
{
Console.WriteLine("No match");
}
0
nice
0
Yeeeees I figured this out !!! It is very tricky . I used the bool variable too but I like the idea of just using count == 0 ... kicking myself now!! Haha! Great puzzle ! It too me days and now I feel like I love these puzzles. What a bizarre feeling when you figure it out and it is so simple!