I’m supposed to take in a string and return true if it is in alphabetical order. So far in my solution I am able to get true from "biopsy" but words like "BeRrY" throw a false. I am assuming this is because some of the characters are capitalized but I don’t know where I am going wrong. This is what I have.
public bool IsAlphabetical(string s)
{
char[] c = s.ToCharArray();
Array.Sort(c);
return (c.SequenceEqual(s));
}
>Solution :
if you need not case-sensitive comparison, you can try this
public static bool IsAlphabetical(string s,bool checkWithoutCase=true)
{
var c = s.ToCharArray();
Array.Sort(c);
var equal = c.SequenceEqual(s);
// check without case
if (!equal && checkWithoutCase)
{
s = s.ToLower();
Array.Sort(c = s.ToCharArray());
equal = c.SequenceEqual(s);
}
return equal;
}