0

Hello, I'm a beginner, I've been trying to solve the Isogram detector challenge in C# for days but I can't. Please Help me

Here is my code it doesnt work i don t understand why { class Program { static void Main(string[] args) { string word=Console.ReadLine(); char[]arr=word.ToCharArray(); string iso="false"; foreach (char i in arr) { if (arr.Count(i)==1) { iso="true"; } } Console.Write(iso); } } }

29th Apr 2023, 7:09 AM
Romain Faure
Romain Faure - avatar
18 ответов
+ 4
oooh, ok apparently Count is one of those methods that takes an anonymous function as its argument, so this is no longer a beginner problem. The correct code for that line ends ups looking something like: if(arr.Count( n => n==i ) == 1) Unless that made sense to you, probably best to abandon .Count and look for an alternative approach
29th Apr 2023, 8:30 AM
Orin Cook
Orin Cook - avatar
+ 3
Yes, an isogram is a word where *every* letter is unique. Your current code prints true if *any* letter is unique. My hint was to reverse your logic: try starting with true and testing for false cases instead.
29th Apr 2023, 7:55 AM
Orin Cook
Orin Cook - avatar
+ 2
Romain Faure Two side hints for better help in the future: 1. Instead of copying and pasting the code, save it in Code Playground and add a link to the code in the question description (use "+" button). This way, we can run and debug your code. Plus, we all avoid copy errors. 2. Avoid excess blank lines. Use a single one between code blocks, and the logic will be clear to who reads it. Too many blank lines make the screen not to show whole code sections and the reader to lose visual references.
29th Apr 2023, 1:31 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Kritagya Pokharel Stop flooding the thread with random comments
3rd May 2023, 9:27 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Currently, your foreach loop is setting iso to true for any word with any non-repeating letters (which is most words, excluding the rare oddball like poop or noon). Try reversing your logic.
29th Apr 2023, 7:28 AM
Orin Cook
Orin Cook - avatar
+ 1
Thank you very much for your help but the isogram is a word who contain no repeating letter and the challenge is to detect which word is an isogram
29th Apr 2023, 7:43 AM
Romain Faure
Romain Faure - avatar
+ 1
Ok ok i m sorry i m trying this thank you
29th Apr 2023, 8:04 AM
Romain Faure
Romain Faure - avatar
+ 1
The same error message for the line if(arr.Count(i)==1) Argument 2: cannot convert from char to system.Func<char,bool>
29th Apr 2023, 8:13 AM
Romain Faure
Romain Faure - avatar
+ 1
It works!!! Thank you very much
29th Apr 2023, 8:39 AM
Romain Faure
Romain Faure - avatar
+ 1
Romain Faure After knowing how to use arr.Count(f=>f==i) How about Googling how to use HashSet? You can compare Hashset.Count to String.Length to determine whether or not it is an isogram. It's another way to solve the problem.
29th Apr 2023, 11:47 PM
Bob_Li
Bob_Li - avatar
+ 1
string word=Console.ReadLine(); var ls = new List<char>(); string iso="false"; //string isalready char array //char[]arr=word.ToCharArray(); //count chars int cc=0; //foreach (char i in arr){ //you cant count its like this.. // if (arr.Count(i)==1) foreach (char i in word){ //check contains it or not.. if(!ls.Contains(i)){ // iso="true"; cc++;//if no so plus one.. } //after add char to list from string ls.Add(i); } //check length if equals so ok, else string has no unique symbols iso = word.Length==cc?"true":"false"; Console.WriteLine(iso); //shortly to use set its can contains only unique, no equals var hs = new HashSet<char>(){}; foreach(var u in word)hs.Add(u); Console.Write( hs.Count()==word.Length?"true":"false"); //its very simple examples.. just for example..
30th Apr 2023, 6:38 AM
Smith Welder
Smith Welder - avatar
+ 1
Smith Welder even shorter way to create HashSet from String var hs = new HashSet<char>(word.ToCharArray());
30th Apr 2023, 6:53 AM
Bob_Li
Bob_Li - avatar
+ 1
Yea, HashSet<char>(word.ToCharArray()),or HashSet<char>(word.ToArray()).. , Bob_Li nice one 👍 xd
30th Apr 2023, 7:09 AM
Smith Welder
Smith Welder - avatar
0
Ok 👌 thx
29th Apr 2023, 1:35 PM
Romain Faure
Romain Faure - avatar
0
Bro i dont kbow it
30th Apr 2023, 1:51 AM
Kritagya Pokharel
0
Try this string word=Console.ReadLine(); var ls = new List<char>(); string iso="false"; //string isalready char array //char[]arr=word.ToCharArray(); //count chars int cc=0; //foreach (char i in arr){ //you cant count its like this.. // if (arr.Count(i)==1) foreach (char i in word){ //check contains it or not.. if(!ls.Contains(i)){ // iso="true"; cc++;//if no so plus one.. } //after add char to list from string ls.Add(i); } //check length if equals so ok, else string has no unique symbols iso = word.Length==cc?"true":"false"; Console.WriteLine(iso); //shortly to use set its can contains only unique, no equals var hs = new HashSet<char>(){}; foreach(var u in word)hs.Add(u); Console.Write( hs.Count()==word.Length?"true":"false"); //its very simple examples.. just for example.. This might help you to learn thank you
1st May 2023, 4:12 AM
mᏒ_ᏒᎪj ᎶuᏒu
mᏒ_ᏒᎪj ᎶuᏒu - avatar
0
Lol bro so much
3rd May 2023, 2:31 AM
Kritagya Pokharel
0
But it works
3rd May 2023, 2:31 AM
Kritagya Pokharel