What is wrong with this piece of code? Need help? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is wrong with this piece of code? Need help?

string question = Convert.ToString(Console.ReadLine()); if(!question.Contains("How" || "Where" || "Why" || "When" || "Which")) { Console.WriteLine("Please ask a question."); } Console.WriteLine(question);

16th Jan 2021, 10:00 AM
aka :)
aka :) - avatar
6 Answers
+ 4
MrDevEzeoke You can store all string in an array and using loop you can check. If any string exist then break the loop. https://code.sololearn.com/c677tEMtcmNK/?ref=app
16th Jan 2021, 10:23 AM
AĶ¢J
AĶ¢J - avatar
+ 5
MrDevEzeoke , what you could do additionally is to spell the "keywords" in lower case: string[] words = {"how", "where", "why", "when", "which"}; then also convert the input to lower case: string question = Console.ReadLine().ToLower(); This makes it possible to find "Why" as well as "why" or whatever case is used.
16th Jan 2021, 3:37 PM
Lothar
Lothar - avatar
+ 3
MrDevEzeoke Can Contains accept multiple String?
16th Jan 2021, 10:03 AM
AĶ¢J
AĶ¢J - avatar
+ 3
Or starting from the array of keywords you can combine Any() and Contains(). To use Any() you have to include Linq. if(Keywords.Any(kw => question.Contains(kw)))
16th Jan 2021, 10:28 AM
Benjamin JĆ¼rgens
Benjamin JĆ¼rgens - avatar
+ 3
Thank you I Am AJ ! , Benjamin JĆ¼rgens and Lothar for your answers.
16th Jan 2021, 10:44 AM
aka :)
aka :) - avatar
+ 1
I Am AJ ! No it can't. I guess that answers my question however how would I check for these multiple strings within the inputed question?
16th Jan 2021, 10:05 AM
aka :)
aka :) - avatar