Java Program to Check Whether the Given String is Pangram

JavaCampus InterviewServer Side ProgrammingProgramming

<p>In this article, we will understand how to check whether the given string is pangram. A string is a pangram string if it contains all the character of the alphabets ignoring the case of the alphabets.</p><p>Below is a demonstration of the same &minus;</p><p><strong>Suppose our input is</strong> &minus;</p><pre class="result notranslate">Input string: Abcdefghijklmnopqrstuvwxyz</pre><p><strong>The desired output would be</strong> &minus;</p><pre class="result notranslate">Yes, the string is a pangram</pre><h2>Algorithm</h2><pre class="result notranslate">Step 1 - START Step 2 - Declare a string value namely input_string. Step 3 - Define the values. Step 4 - Convert the input string to a character array. Step 5 - Iterate over the character of the array and check if the array contains all the alphabets using charAt(i) - &#39;a&#39;. If yes, it&rsquo;s a Pangram string. Step 6 - Display the result Step 7 - Stop</pre><h2>Example 1</h2><p>Here, we bind all the operations together under the &lsquo;main&rsquo; function.</p><pre class="demo-code notranslate language-java" data-lang="java">public class Pangram { &nbsp; &nbsp;static int size = 26; &nbsp; &nbsp;static boolean isLetter(char ch) { &nbsp; &nbsp; &nbsp; if (!Character.isLetter(ch)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false; &nbsp; &nbsp; &nbsp; return true; &nbsp; &nbsp;} &nbsp; &nbsp;public static void main(String args[]) { &nbsp; &nbsp; &nbsp; String input_string = &quot;Abcdefghijklmnopqrstuvwxyz&quot;; &nbsp; &nbsp; &nbsp; System.out.println(&quot;The string is defined as: &quot; +input_string); &nbsp; &nbsp; &nbsp; int string_length = input_string.length(); &nbsp; &nbsp; &nbsp; input_string = input_string.toLowerCase(); &nbsp; &nbsp; &nbsp; boolean[] is_true = new boolean[size]; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; string_length; i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (isLetter(input_string.charAt(i))) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int letter = input_string.charAt(i) - &#39;a&#39;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is_true[letter] = true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; boolean result; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; size; i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!is_true[i]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = false; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; result = true; &nbsp; &nbsp; &nbsp; if (result) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot; Yes, the string is a pangram&quot;); &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot; No, the string is not a pangram&quot;); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">The string is defined as: Abcdefghijklmnopqrstuvwxyz Yes, the string is a pangram</pre><h2>Example 2</h2><p>Here, we encapsulate the operations into functions exhibiting object-oriented programming.</p><pre class="demo-code notranslate language-java" data-lang="java">public class Pangram { &nbsp; &nbsp;static int size = 26; &nbsp; &nbsp;static boolean isLetter(char ch) { &nbsp; &nbsp; &nbsp; if (!Character.isLetter(ch)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true; &nbsp; &nbsp;} &nbsp; &nbsp;static boolean check_alphabets(String input_string, int string_length) { &nbsp; &nbsp; &nbsp; input_string = input_string.toLowerCase(); &nbsp; &nbsp; &nbsp; boolean[] is_true = new boolean[size]; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; string_length; i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (isLetter(input_string.charAt(i))) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int letter = input_string.charAt(i) - &#39;a&#39;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is_true[letter] = true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; size; i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!is_true[i]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; return true; &nbsp; &nbsp;} &nbsp; &nbsp;public static void main(String args[]) { &nbsp; &nbsp;String input_string = &quot;Abcdefghijklmnopqrstuvwxyz&quot;; &nbsp; &nbsp;System.out.println(&quot;The string is defined as: &quot; +input_string); &nbsp; &nbsp;int string_length = input_string.length(); &nbsp; &nbsp;if (check_alphabets(input_string, string_length)) &nbsp; &nbsp; &nbsp; System.out.println(&quot; Yes, the string is a pangram&quot;); &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; System.out.println(&quot; No, the string is not a pangram&quot;); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">The string is defined as: Abcdefghijklmnopqrstuvwxyz Yes, the string is a pangram</pre>
raja
Updated on 29-Mar-2022 11:30:38

Advertisements