Commit b21a03d2 authored by Dr. Martin Goik's avatar Dr. Martin Goik

New pangram exercise

parent a0282707
......@@ -829,6 +829,72 @@ Mark|frequency
<qandaset defaultlabel="qanda" xml:id="sd1QandaPangram">
<title><acronym>Pangram</acronym> checker</title>
<para>A <acronym>pangram</acronym> is a string containing all
letters of a given alphabet at least once. As an example we consider
the alphabet of <xref linkend="glo_ASCII"/> letters <code>{'a', 'b',
..., 'z'}</code> ignoring case. Then the following string is a
<para>The quick brown fox jumps over the lazy dog</para>
<para>Like in <xref linkend="sd1QandaMarkFrequencies"/> create
an array of sufficient size regarding each character as an
index value.</para>
<para>According to the <xref linkend="glo_ASCII"/> table all
characters <code>{'a', 'b', ..., 'z'}</code> form a gapless
sequence of integer values {97, 98,..., 122}.</para>
allows for de-composing a String into a corresponding array of
char values.</para>
<para>Normalize strings with respect to case using <methodname
<annotation role="make">
<para role="eclipse">P/Sd1/Array/pangram</para>
<para>In addition to the above <quote>regular</quote> solution we
also provide an approach featuring <xref linkend="glo_Java"/>
streams with respect to the upcoming <quote>Software development
2</quote> lecture:</para>
<programlisting language="java">static public boolean isPangram2(final String s) {
return 26 == s.toLowerCase().chars()
.filter(i -&gt; i &gt;= 'a' &amp;&amp; i &lt;= 'z')
<figure xml:id="sd1_array_fig_arrayDefinition">
<project xmlns="" xmlns:xsi=""
package de.hdm_stuttgart.mi.pangram;
* <p>Checking whether a given ASCII String is a pangram.</p>
class PangramChecker {
static private final byte alphabetCharCount = 'z' - 'a' + 1; // 26 characters {'a', 'b', ..., 'z'}
static private final char alphabetStartIndex = 'a';
* <p>Is a given string a pangram with respect to the set of characters {'a', 'b', ..., 'z'} or not?</p>
* @param s String to be evaluated.
* @return true if string is a pangram, false otherwise.
static public boolean isPangram(final String s) {
final boolean[] charactersFound = // Reserve one boolean per alphabet character, initialized to false.
new boolean[alphabetCharCount];
int distinctCharacterCount = 0; // Number of different characters found so far.
for (final char c : s.toLowerCase().toCharArray()) {
if ('a' <= c && c <= 'z' // Character belongs to alphabet?
&& !charactersFound[c - 'a']) { // Character already discovered during call?
distinctCharacterCount++; // Yet another distinct one.
charactersFound[c - 'a'] = true; // Mark alphabet character as being found.
return distinctCharacterCount == alphabetCharCount; // Did we find all 26?
* Same as {@link #isPangram(String)} using a stream based implementation
* @param s See {@link #isPangram(String)}
* @return See {@link #isPangram(String)}
static public boolean isPangram2(final String s) {
return alphabetCharCount == s.toLowerCase().chars()
.filter(i -> i >= 'a' && i <= 'z')
* Main entry point
* @param args Unused.
public static void main(String[] args) {
System.out.println(isPangram("The quick brown fox jumps over the lazy dog"));
System.out.println(isPangram("The quick brown fox jumps over the lazy"));
package de.hdm_stuttgart.mi.pangram;
import org.junit.Assert;
import org.junit.Test;
* Hunting for pangrammatic strings.
public class PangramTest {
public void testNegative() {
Assert.assertFalse(PangramChecker.isPangram("Two driven jocks help fax my big"));
Assert.assertFalse(PangramChecker.isPangram("Pack box with five dozen liquor jugs"));
Assert.assertFalse(PangramChecker.isPangram(" five boxing wizards jump quickly"));
Assert.assertFalse(PangramChecker.isPangram("Bright vixens jump; dozy quack"));
Assert.assertFalse(PangramChecker.isPangram("Jackdaws love my big sphinx quartz"));
Assert.assertFalse(PangramChecker.isPangram("John quickly extemporized five bags"));
Assert.assertFalse(PangramChecker.isPangram("Waltz, nymph, for quick jigs Bud"));
Assert.assertFalse(PangramChecker.isPangram("Quick wafting zephyrs bold Jim"));
Assert.assertFalse(PangramChecker.isPangram("Brown jars prevented mixture from freezing uickly"));
Assert.assertFalse(PangramChecker.isPangram("Fred specialized the job of making very quaint toys"));
Assert.assertFalse(PangramChecker.isPangram("Sixty zippers were quickly picked from woven jute bag"));
Assert.assertFalse(PangramChecker.isPangram("We promptly judged antique ivory buckles for next prize"));
Assert.assertFalse(PangramChecker.isPangram("J.Q. Schwartz flung V.D. Pike box"));
Assert.assertFalse(PangramChecker.isPangram("Viewing quizzical abstracts mixed hefty jocks"));
Assert.assertFalse(PangramChecker.isPangram("Farmer jack realized that yellow quilts were expensive"));
Assert.assertFalse(PangramChecker.isPangram("My girl wove dozen plaid jackets before she quit"));
Assert.assertFalse(PangramChecker.isPangram("Warm plucky G.H.Q. jinx, to B.V.D.'s"));
public void testPositive() {
// Credits to
Assert.assertTrue(PangramChecker.isPangram("Two driven jocks help fax my big quiz"));
Assert.assertTrue(PangramChecker.isPangram("Pack my box with five dozen liquor jugs"));
Assert.assertTrue(PangramChecker.isPangram("The five boxing wizards jump quickly"));
Assert.assertTrue(PangramChecker.isPangram("Bright vixens jump; dozy fowl quack"));
Assert.assertTrue(PangramChecker.isPangram("Jackdaws love my big sphinx of quartz"));
Assert.assertTrue(PangramChecker.isPangram("John quickly extemporized five tow bags"));
Assert.assertTrue(PangramChecker.isPangram("Waltz, nymph, for quick jigs vex Bud"));
Assert.assertTrue(PangramChecker.isPangram("Quick wafting zephyrs vex bold Jim"));
Assert.assertTrue(PangramChecker.isPangram("Brown jars prevented the mixture from freezing too quickly"));
Assert.assertTrue(PangramChecker.isPangram("Fred specialized in the job of making very quaint wax toys"));
Assert.assertTrue(PangramChecker.isPangram("Sixty zippers were quickly picked from the woven jute bag"));
Assert.assertTrue(PangramChecker.isPangram("We promptly judged antique ivory buckles for the next prize"));
Assert.assertTrue(PangramChecker.isPangram("J.Q. Schwartz flung V.D. Pike my box"));
Assert.assertTrue(PangramChecker.isPangram("Viewing quizzical abstracts mixed up hefty jocks"));
Assert.assertTrue(PangramChecker.isPangram("Farmer jack realized that big yellow quilts were expensive"));
Assert.assertTrue(PangramChecker.isPangram("My girl wove six dozen plaid jackets before she quit"));
Assert.assertTrue(PangramChecker.isPangram("Warm plucky G.H.Q. jinx, fez to B.V.D.'s"));
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment