How to resolve the algorithm Pangram checker step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Pangram checker step by step in the D programming language

Table of Contents

Problem Statement

A pangram is a sentence that contains all the letters of the English alphabet at least once. For example:   The quick brown fox jumps over the lazy dog.

Write a function or method to check a sentence to see if it is a   pangram   (or not)   and show its use.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pangram checker step by step in the D programming language

Source code in the d programming language

bool isPangram(in string text) pure nothrow @safe @nogc {
    uint bitset;

    foreach (immutable c; text) {
        if (c >= 'a' && c <= 'z')
            bitset |= (1u << (c - 'a'));
        else if (c >= 'A' && c <= 'Z')
            bitset |= (1u << (c - 'A'));
    }

    return bitset == 0b11_11111111_11111111_11111111;
}

void main() {
    assert("the quick brown fox jumps over the lazy dog".isPangram);
    assert(!"ABCDEFGHIJKLMNOPQSTUVWXYZ".isPangram);
    assert(!"ABCDEFGHIJKL.NOPQRSTUVWXYZ".isPangram);
    assert("ABC.D.E.FGHI*J/KL-M+NO*PQ R\nSTUVWXYZ".isPangram);
}


import std.string, std.traits, std.uni;

// Do not compile with -g (debug info).
enum Alphabet : dstring {
    DE = "abcdefghijklmnopqrstuvwxyzßäöü",
    EN = "abcdefghijklmnopqrstuvwxyz",
    SV = "abcdefghijklmnopqrstuvwxyzåäö"
}

bool isPangram(S)(in S s, dstring alpha = Alphabet.EN)
pure /*nothrow*/ if (isSomeString!S) {
    foreach (dchar c; alpha)
       if (indexOf(s, c) == -1 && indexOf(s, std.uni.toUpper(c)) == -1)
            return false;
    return true;
}

void main() {
    assert(isPangram("the quick brown fox jumps over the lazy dog".dup, Alphabet.EN));
    assert(isPangram("Falsches Üben von Xylophonmusik quält jeden größeren Zwerg"d, Alphabet.DE));
    assert(isPangram("Yxskaftbud, ge vår wczonmö iqhjälp"w, Alphabet.SV));
}


  

You may also check:How to resolve the algorithm SEDOLs step by step in the SQL PL programming language
You may also check:How to resolve the algorithm Negative base numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Fermat numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Haskell programming language
You may also check:How to resolve the algorithm Tau number step by step in the Julia programming language