C# program to check whether a given string is Heterogram or not

CsharpProgrammingServer Side Programming

Heterogram for a string means the string isn’t having duplicate letters. For example −

Mobile
Cry
Laptop

Loop through each word of the string until the length of the string −

for (int i = 0; i < len; i++) {
   if (val[str[i] - 'a'] == 0)
   val[str[i] - 'a'] = 1;
   else
   return false;
}

Above, len is the length of the string.

Let us see the complete code −

Example

 Live Demo

using System;

public class GFG {
   static bool checkHeterogram(string str, int len) {
      int []val = new int[26];

      for (int i = 0; i < len; i++) {
         if (val[str[i] - 'a'] == 0)
         val[str[i] - 'a'] = 1;
         else
         return false;
      }
      return true;
   }
   public static void Main () {
      string str = "mobile";

      // length of the entered string
      int len = str.Length;
      if(checkHeterogram(str, len))
      Console.WriteLine("String is Heterogram!");
      else
      Console.WriteLine("String is not a Heterogram!");
   }
}

Output

String is Heterogram!
raja
Published on 31-Aug-2018 16:04:10
Advertisements