+ 1

C# "Words" code project help

I'm trying to take a letter as input, iterate through an array of strings and output all words that contain the letter. This is my code, I'm not sure what I'm missing. Thank you! 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; for (count =0; count <= words.Length; count++); { if (words.Contains(letter)){ Console.Write(words[count]); } else { Console.Write("No match"); } }

30th Aug 2022, 6:41 PM
MAB
MAB - avatar
32 odpowiedzi
+ 3
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 matches = 0; for(int cnt=0; cnt < words.Length; cnt++) { if (words[ cnt ].Contains(letter)){ Console.WriteLine(words[ cnt ]); matches++; } } if( matches == 0 ) Console.Write("No match"); } } } /* And by for each loop : foreach( string st in words) { if ( st.Contains(letter) ){ Console.WriteLine(st); matches++; } } */ //ok?
1st Sep 2022, 8:37 PM
Jayakrishna 🇮🇳
+ 4
Remove semicolon after for loop )//; And condition should be count < words.Length // use < , not <=. and oh. one more : if( words[count].contains(letter) ){
30th Aug 2022, 6:48 PM
Jayakrishna 🇮🇳
+ 3
foreach(string i in words) { if(i.Contains(letter)){ Console.WriteLine(i); count++; } }
1st Sep 2022, 8:38 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
I don't know if it's a peculiarity of Sololearn's code render, but I changed your loop a bit and it prints a lot of "no match" and then the word that matches. So you'll have to think about adding something to the else condition that isn't the print and only print "no match" when there isn't a match. https://code.sololearn.com/cUli67GpiRUg/?ref=app
31st Aug 2022, 9:54 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Jayakrishna🇮🇳 it's the end of the arrays section in C#. I think section 49, a code coach...?
31st Aug 2022, 1:48 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
"All I need to do is figure out the code to have it output "no match" once after it reaches the end of the array." 100% When I did it, I had another integer that counted the matches.
31st Aug 2022, 6:02 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Don't worry, some of these codes are tricky brain teasers! Your if statement looks great! I think there's something wrong with your else if statement and you might be better without it. So, only have your if statement. No else or else if. After that, create a new if and if your count value equals 0, print "no match".
31st Aug 2022, 6:45 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
You're right MAB. I tested with "m" and received some words but not all! Same as "o"!! It should work. In my learning solution, I used "foreach" instead of the "for" loop. All green ticks, but I don't see why there is a difference.
31st Aug 2022, 9:49 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
You missing i++; in loop increment place. So it infinite loop.. Don't add in if block. Ausgrindtube your code may goes infinite loop if on any unmatch.. You should add i++ in loop. MAB try now.. If not work, I post full correct code if you want.. ! hope it helps....
1st Sep 2022, 8:56 AM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 do you mean in the foreach loop? It worked in my code coach with no problem. As I understand it, the foreach loop is with all the conditions of a for loop.
1st Sep 2022, 2:03 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Ahhh! You're totally right Jayakrishna🇮🇳 !! How could I have missed the placement of the i++. Thanks for your careful eye. The code has been updated.
1st Sep 2022, 5:30 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
@MAB You have instead a count variable. But conditions using are wrong.. You can use a boolean variable also like bool match = false; And in if block change to match = true; Then do after loop if( !match ) Console.Write("No match"); You're welcome...
2nd Sep 2022, 8:04 AM
Jayakrishna 🇮🇳
+ 1
Okay, this is a great start! I'm outputting just "no match," though, no matter the input: string letter = Console.ReadLine(); int count = 0; for (count =0; count < words.Length; count++){ if (words[count].Contains(letter)){ Console.Write(words[count]); break; } else { Console.Write("No match"); break;
30th Aug 2022, 8:17 PM
MAB
MAB - avatar
+ 1
You are breaking loop after first word checking... Then no use of loop.. What is your actual task..? Lesson no:?
31st Aug 2022, 12:00 PM
Jayakrishna 🇮🇳
+ 1
MAB Ok. Remove the break statement in loop. The task is asked to check input letter is present in array elements. If there, print that array element. So you need to check all 10 elements in array.. If the none of array element has the input letter, ( at least 1 is no match) then only print "no match" after loop.. So no need else part in loop. No need break in if part... Ausgrindtube thank you for info..
31st Aug 2022, 4:17 PM
Jayakrishna 🇮🇳
+ 1
Looking into it, I think it's outputting "No match" between matches because it's outputting "no match" for each item in the array. All I need to do is figure out the code to have it output "no match" once after it reaches the end of the array.
31st Aug 2022, 5:50 PM
MAB
MAB - avatar
+ 1
Would that be the int count then? I don't understand how to connect "counting the matches" to editing the else statement? Your feedback has been helpful, I'm just really stuck. Would it be something like this? string[] words = { "home", "programming", "victory", "C#", "football", "sport", "book", "learn", "dream", "fun" }; string letter = Console.ReadLine(); int count = 0; for (int i = 0; i < words.Length; i++) { if (words[i].Contains(letter)) { Console.Write(words[i]); count++; } else if (count == words.Length) {Console.Write("No match");}
31st Aug 2022, 6:13 PM
MAB
MAB - avatar
+ 1
Ok, getting closer, but still not outputting properly. My code looks like this now. It outputs "No match" during regular test cases if I do this, but not when there's no matches: string letter = Console.ReadLine(); int count = 0; for (int i = 0; i < words.Length; i++) { if (words[i].Contains(letter)) { Console.Write(words[i]); count++; } if(count == words.Length) {Console.Write("No match"); }
31st Aug 2022, 7:09 PM
MAB
MAB - avatar
+ 1
See if you can see the difference. ;) https://code.sololearn.com/cUli67GpiRUg/?ref=app
31st Aug 2022, 7:23 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Testing your code, entering "z" produces no output, but entering "m" doesn't print all results? I'm not sure what's going on.
31st Aug 2022, 8:14 PM
MAB
MAB - avatar