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.

22nd Dec 2020, 11:35 AM
VeteranDeFoo
VeteranDeFoo - avatar
23 Answers
+ 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"); } } } }
23rd Jan 2021, 12:09 AM
Ana Lucia Ferreira Gomes
Ana Lucia Ferreira Gomes - avatar
+ 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"); } } } }
8th Jan 2022, 11:22 PM
Mostafa Fayazi
Mostafa Fayazi - avatar
+ 4
I changed your code، Use lowercase letters. https://code.sololearn.com/c6PzeVWHMJKE/?ref=app
22nd Dec 2020, 7:28 PM
hossein B
hossein B - avatar
+ 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.
22nd Dec 2020, 11:57 AM
Gordon
Gordon - avatar
+ 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"); }
20th Jan 2021, 2:07 PM
Hamza Arbi
Hamza Arbi - avatar
+ 2
MrDevEzeoke , show your attempt so somebody can help you.
22nd Dec 2020, 11:39 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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.
22nd Dec 2020, 12:07 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 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"); }
28th Feb 2022, 1:06 PM
Spower
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.
22nd Dec 2020, 11:40 AM
VeteranDeFoo
VeteranDeFoo - avatar
0
MrDevEzeoke , please link your code to the Playground.
22nd Dec 2020, 11:42 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
I do not know how with a project. TheWh¡teCat 🇧🇬 could you tell me how?
22nd Dec 2020, 11:45 AM
VeteranDeFoo
VeteranDeFoo - avatar
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.
22nd Dec 2020, 11:48 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
22nd Dec 2020, 11:51 AM
VeteranDeFoo
VeteranDeFoo - avatar
0
Gordon Thank you I understand now. TheWh¡teCat 🇧🇬 I did that Thank you.
22nd Dec 2020, 11:57 AM
VeteranDeFoo
VeteranDeFoo - avatar
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");
26th Jan 2021, 9:50 AM
Victoria Colta
Victoria Colta - 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; 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"); } } } }
7th Feb 2021, 5:23 PM
Md Momin Uddin Hridoy
Md Momin Uddin Hridoy - 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 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 Aug 2021, 6:40 PM
رسول
رسول - avatar
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"); }
20th Sep 2021, 1:42 AM
Gizachew Cs
Gizachew Cs - avatar
0
nice
25th Feb 2022, 7:11 AM
Jehliel Bothma
Jehliel Bothma - avatar
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!
2nd Oct 2022, 1:21 PM
Gregory Davies