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); } } }
18 Réponses
+ 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
+ 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.
+ 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.
+ 2
Kritagya Pokharel Stop flooding the thread with random comments
+ 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.
+ 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
+ 1
Ok ok i m sorry i m trying this thank you
+ 1
The same error message for the line if(arr.Count(i)==1)
Argument 2: cannot convert from char to system.Func<char,bool>
+ 1
It works!!! Thank you very much
+ 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.
+ 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..
+ 1
Smith Welder
even shorter way to create HashSet from String
var hs = new HashSet<char>(word.ToCharArray());
+ 1
Yea, HashSet<char>(word.ToCharArray()),or HashSet<char>(word.ToArray()).. , Bob_Li nice one 👍 xd
0
Ok 👌 thx
0
Bro i dont kbow it
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
0
Lol bro so much
0
But it works