Home > Mobile >  I need to check if a string is a pangram string or not is my code correct?
I need to check if a string is a pangram string or not is my code correct?

Time:09-10

public static class Inova
{
    public static bool IsPangram(string str)
    {
        int compteur = 26;

        for (int i = 0; i <= str.Length; i  )
        {

            if (('A' <= str[i] && str[i] <= 'Z') || ('a' <= str[i] && str[i] <= 'z'))
            {
                for (int j = str[i   1]; j <= str.Length; j  )
                {
                    if (compteur != 0 && str[i] != str[j])
                    {
                        compteur = compteur - 1;
                    }
                }

            }

            if (compteur == 0)
            {
                return true;
            }

            else
            {
                return false;
            }
        }
    }
}

CodePudding user response:

There are multiple things incorrect:

  • for (int j = str[i 1]; j <= str.Length; j ) this does not do what you think, it will convert the next char to an int, you want to loop all letters until end, beginning from the current letter 1.
  • The if ... else belong to the end of the method, outside of the loop, otherwise you return false after the first iteration in the for-loop

So you want to know if it's a perfect pangram? First we need to say what a pangram is: a sentence containing every letter of the alphabet. It seems you want to check if it's even a perfect pangram, so every letter should appear exactly once. Here is a method not using any fancy LINQ(which might not be allowed) that supports perfect/imperfect pangrams:

public static class Inova
{
    private const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   
    public static bool IsPangram(string str, bool mustBePerfect)
    {
        HashSet<char> remaingLetters = new HashSet<char>(alphabet);
        foreach (char c in str)
        {
            char letter = char.ToUpperInvariant(c);
            if (!alphabet.Contains(letter)) continue;
            bool repeatingLetter = !remaingLetters.Remove(letter);
            if (mustBePerfect && repeatingLetter)
            {
                return false; // already removed
            }
        }

        return remaingLetters.Count == 0;
    }
}

Usage:

bool isPangram = Inova.IsPangram("abcdefghijklmnopqrstuvwxyZZ", false); 

Since z appears twice this method returns false for perfect and true for not perfect.

Demo: https://dotnetfiddle.net/gEXuvG

Side-note: i wanted to keep it simple, if you want you can still improve it. You can return true in the loop: if(!mustBePerfect && remaingLetters.Count == 0) return true.

CodePudding user response:

I would check for existence of each letter in the string, so

public static bool IsPangram(string str) {

  str = str.ToLower();

  for (int i = 0; i < 26; i  ) {

    char c = Convert.ToChar(i   97);

    if (!str.Contains(c)) {
      return false;
    }
  }
  return true;
}


Console.WriteLine(IsPangram("hello world"));
Console.WriteLine(IsPangram("abcdefghi jkl mno pqrstuvwxyz"));

// output:
False
True
  • Related