Home > Back-end >  Comparing characters in a string in C#
Comparing characters in a string in C#

Time:02-19

I am trying to write a method that will determine whether a string that contains only letters is an isogram. (An isogram is a word that has no repeating letters, consecutive or non-consecutive.) Here's my code:

***static bool IsIsogram(string str)
{
    for (int i = 0; i <= str.Length; i  )
    {
        for (int j = 0; j < i; j  )
        {
            if (string.Compare("str[i]", "str[j]") == 0)
            {
                return false;
            }
            else if (string.Compare("str[i]", "str[j]") == 1)
            {
                return true;
            }
        }
    }

}

IsIsogram("Hello");***

EDIT: How about this:

static bool IsIsogram(string str) { 
   foreach (char c in str) { 
      for (int i = 0;i<=str.Length;i  ) { 
         if (c == str[i]) { 
            return false; 
         } 
         else { 
            return true; 
         } 
      } 
   } 
} 
IsIsogram("Hello");

CodePudding user response:


The code in the edit has two issues. First, it only checks the first character and always returns a value, without actually checking the whole string. The function should only stop checking when it has already determined that the string is not an isogram. To put that simply, the return true should be after both loops have finished.


The second problem is that it checks every character in the string, including the current one. Basically, if the input is "Bar", it will first check if 'B' == 'B', and then think B occurs twice when it really is only once. To fix this, you will need to skip the current character. In code, that would look something like if (i == j) continue;

static bool IsIsogram(string str) { 
    for (int j = 0; j < str.Length; j  ) { 
       for (int i = 0; i < str.Length; i  ) { 
          if (j == i) continue;
          if (str[j] == str[i]) {
            return false; 
          }
       } 
    }
    return true;
}

This code could also be optimized by setting i to j 1 instead of 0, because there is no need to check characters that have already been checked. This would also remove the need to check if (i == j) in the loop, because i can never be equal to j if i >= j 1

Psuedo code (Untested):

static bool IsIsogram(string str) { 
    for (int j = 0; j < str.Length; j  ) { 
        for (int i = j   1; i < str.Length; i  ) { 
            if (str[j] == str[i]) {
                return false; 
            }
        } 
    }
    return true;
}

CodePudding user response:

All you really want to do is check if there are any repeated characters after you set letters to capital/lower to compare.

static bool IsIsogram(string str)
{
    return str.ToLower().Distinct().Count() == str.Length;
}

CodePudding user response:

Try this,

string str = "hello";
int[] ival = str.Select(a => Convert.ToInt32(a)).ToArray();
Array.Sort(ival);
for(int i=0; i<ival.Count()-1; i  ) 
{
 if (ival[i]==ival[i 1])
 {
  Console.WriteLine("isogram");
  break;
 }
}
  • Related