August 10, 2018

Given String is Pangram or not?


Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet.
Examples : The quick brown fox jumps over the lazy dog ” is a Pangram [Contains all the characters from ‘a’ to ‘z’]
“The quick brown fox jumps over the dog” is not a Pangram [Doesn’t contains all the characters from ‘a’ to ‘z’, as ‘l’, ‘z’, ‘y’ are missing]
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
We create a mark[] array of Boolean type. We iterate through all the characters of our string and whenever we see a character we mark it. Lowercase and Uppercase are considered the same. So ‘A’ and ‘a’ are marked in index 0 and similarly ‘Z’ and ‘z’ are marked in index 25.
After iterating through all the characters we check whether all the characters are marked or not. If not then return false as this is not a pangram else return true.

Program:

public class Panagram {


public static void main(String[] args) {
String input = "The quick brown fox jumps over the lazy dog";
System.out.println("Panagram test string : "+input);

System.out.println(""+ checkPanagram(input));
}

/**
*Returns true if the string     is pangram else false
* @param input
* @return
*/
private static String checkPanagram(String input) {
String result = "Given string is not panagram";
  // Create a hash table to mark the
        // characters present in the string
        // By default all the elements of
        // mark would be false.
boolean[] mark = new boolean[26];

if(input == null) {
return "Given input is invalid";
}
// For indexing in mark[]
int index = 0;
// Traverse all characters
for(int i=0;i // If uppercase character, subtract 'A'
            // to find index.
if( 'A' <= input.charAt(i) && input.charAt(i)  <= 'Z') {
index = input.charAt(i) - 'A';
System.out.println("current value: "+input.charAt(i)+", index :"+index);
}
// If lowercase character, subtract 'a'
            // to find index.
if( 'a' <= input.charAt(i) && input.charAt(i)  <= 'z') {
index = input.charAt(i) - 'a';
System.out.println("current value: "+input.charAt(i)+", index :"+index);
}
// Mark current character
mark[index] = true;

}
System.out.println("mark: "+mark+", length: "+mark.length);
// Return false if any character is unmarked
for(int j =0;j if(mark[j] == false) {
return result;
}
}
// If all characters were present
return "The Given String is panagram.";
}

}



Output:
Panagram test string : The quick brown fox jumps over the lazy dog
current value: T, index :19
current value: h, index :7
current value: e, index :4
current value: q, index :16
current value: u, index :20
current value: i, index :8
current value: c, index :2
current value: k, index :10
current value: b, index :1
current value: r, index :17
current value: o, index :14
current value: w, index :22
current value: n, index :13
current value: f, index :5
current value: o, index :14
current value: x, index :23
current value: j, index :9
current value: u, index :20
current value: m, index :12
current value: p, index :15
current value: s, index :18
current value: o, index :14
current value: v, index :21
current value: e, index :4
current value: r, index :17
current value: t, index :19
current value: h, index :7
current value: e, index :4
current value: l, index :11
current value: a, index :0
current value: z, index :25
current value: y, index :24
current value: d, index :3
current value: o, index :14
current value: g, index :6
mark: [Z@70dea4e, length: 26
The Given String is panagram.


References:
https://www.geeksforgeeks.org/pangram-checking/

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts