Tuesday, May 30, 2023
HomeSoftware EngineeringDetect Pangram in Java | Discover ways to Grasp Software program Engineering,...

Detect Pangram in Java | Discover ways to Grasp Software program Engineering, DevOps and Cloud


The problem

A pangram is a sentence that incorporates each single letter of the alphabet no less than as soon as. For instance, the sentence “The fast brown fox jumps over the lazy canine” is a pangram, as a result of it makes use of the letters A-Z no less than as soon as (case is irrelevant).

Given a string, detect whether or not or not it’s a pangram. Return True whether it is, False if not. Ignore numbers and punctuation.

The answer in Java code

Possibility 1 (utilizing a Character loop):

public class PangramChecker {
    public boolean examine(String sentence){
      for (char c = 'a'; c<='z'; c++)
        if (!sentence.toLowerCase().incorporates("" + c))
          return false;
      return true;
    }
}

Possibility 2 (utilizing a purposeful programming to cut back and rely the distinct characters):

class PangramChecker {
    boolean examine(remaining String sentence) {
        return sentence.chars()
            .filter(Character::isLetter)
            .map(Character::toLowerCase)
            .distinct()
            .rely() == 26;
    }
}

Possibility 3 (utilizing an express examine on all of the characters):

public class PangramChecker {
  public boolean examine(String sentence){
        String x = sentence.toLowerCase();
        if(x.incorporates("a") && x.incorporates("b") && x.incorporates("c") && x.incorporates("d")
                && x.incorporates("e") && x.incorporates("f") && x.incorporates("g")
                && x.incorporates("h") && x.incorporates("i") && x.incorporates("j")
                && x.incorporates("ok") && x.incorporates("l") && x.incorporates("m")
                && x.incorporates("n") && x.incorporates("o") && x.incorporates("p")
                && x.incorporates("q") && x.incorporates("r") && x.incorporates("s")
                && x.incorporates("t") && x.incorporates("u") && x.incorporates("v")
                && x.incorporates("w") && x.incorporates("x") && x.incorporates("y")
                && x.incorporates("z")){
            return true;
        }else{
            return false;
        }
    }
}

Take a look at instances to validate our resolution

import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;


public class PangramTest {
    @Take a look at
    public void test1() {
      String pangram1 = "The fast brown fox jumps over the lazy canine.";
      PangramChecker laptop = new PangramChecker();
      assertEquals(true, laptop.examine(pangram1));
    }
    @Take a look at
    public void test2() {
      String pangram2 = "You shall not cross!";
      PangramChecker laptop = new PangramChecker();
      assertEquals(false, laptop.examine(pangram2));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments