Android Development:First Code Chellange "Pangram"

The quest for learning programming is ON. In my last blogpost i have written few things about the beginning of learning to code. The substance which is significant in the last writing is about how install basic tools to start working with Android Programming. If you have not installed the tools or don't know how to start, the best idea would be to visit my last BlogPost.


What are going to do here? I thin there is no progress in programming without having a challenge. So our first code challenge is to write down a program which find out whether the input sentence is Pangram or not. In summary it means that we will take input from the user and process it and give him the result as an output.

Pangrams are basically sentences which include all the alphabets. The program has to find out that any given sentence is pangram or not. Before jumping into the code it is important to inform you that at first you should try yourself. But in case you are stuck then my explanation of the code might help you.

My machine has a setup of Linux OS, Android Studio, Java JDK and Genymotion. Lets jump to the code directly without wasting any time.

public String pengram(String sentence){
    char[] alphabets = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    //sentence = "pack my box with five dozen liquor jugs";    int a=0;    int b=0;
    char[] sentenceArray = sentence.toCharArray();


    for(int i=0; i<sentenceArray.length*alphabets.length; i++){

        if(alphabets[a]!=sentenceArray[b] && sentenceArray.length-1==b){
            sentence="This is not a pangram";            break;
        }else if(alphabets[a] == sentenceArray[b] && a==alphabets.length-1){
            sentence="This is a pangram";            break;
        }else if(alphabets[a]!=sentenceArray[b]){
            b++;
        }else if(alphabets[a]==sentenceArray[b]){
            a++;            b=0;        }

    }



    return sentence;} 
 
Ok I don't want to make things complex here, i have avoided some of the code specific to android. The aim here is to just explain the logical aspect oft the program. After explanation i will go into the details that how can you give input and take output back in the form of an Android app.

How to create Logic? We need a program which can detect that a given sentence is pangram or not. I have developed the logic step by step. In the above program i have taken the sentence variable in side the function "pengram".
public String pengram(String sentence)

for example the variable sentence is equal to a pangram sentence which might be "pack my bag with five dozen liquor jugs". We need to make logic so that our program can output that this sentence is actually pangram.

Step 1

Declared a character array of alphabets so that we can compare the given sentence to it.
char[] alphabets = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

Step 2

We have to convert the input sentence into a an array as well because we can only take Stirngs as input. To convert string to char array there is inbuilt function in Java. 
char[] sentenceArray = sentence.toCharArray(); 

Step 3

Now we have two character arrays "Alphabets" and "sentenceArray". And it is now easy to compare them. Here comes the critical part and we have two options.
Option 1:To compare sentence with alphabets.
Option 2:To compare Alphabets to sentence.

I have compared alphabets with sentence and i will give the reason in the coming text that i have done this(It is necessary here to note it down).  

Step 4

As i have to compare arrays the obvious choice was to use for loops. They are very powerful for such situations.
  for(int i=0; i<sentenceArray.length*alphabets.length; i++)

If you note down i have used condition as i<Number(comes out to be length of multiplication of two arrays). while comparison loop has to run at least through all the characters of alphabets which are 26 in total. But when the alphabet is found it starts the search again by working from the start of pangram sentence.

Steps 5

So far we have compared the two arrays, sentenceArray(possible pangram sentence) and alphabet array(Alphabets from A-Z). but that is not enough and we find alphabets one by one in sentence. Here comes the first IF condition.
if(alphabets[a]!=sentenceArray[b] && sentenceArray.length-1==b){
            sentence="This is not a pangram";            break;

The "if" statement here is saying that if alphabets is not equal to sentenceArray and sentence array has been reached to the last element then it is not a pangram. In other words if we are reached to the last word of possible pangram but could not find it equal to a particular alphabet then it is not a pangram. And finally "break" the loops. 

Step 6

In the second "else if" condition the program is trying to find out that the give sentence is pangram.
else if(alphabets[a] == sentenceArray[b] && a==alphabets.length-1){
            sentence="This is a pangram";            break;

It says that a given program is pangram if and only if we have been reached to the last alphabet of an array alphabet and it has been matched to some element of given sentence. As the program is designed to compare each alphabet to all the characters of the given sentence; when it reaches the end of alphabets at Z it means that all the previous elements have been found. If the last element 'Z' is also available then it is pangram indeed.

Steps 7

In the last step the logic is defined in way that 
else if(alphabets[a]!=sentenceArray[b]){
            b++;
        }else if(alphabets[a]==sentenceArray[b]){
            a++;            b=0;        }
it tell the loop to compare each element of alphabets to all the characters of sentence, if match is not found move to the next character of sentence until there is a match. Once match is found move forward to next element of alphabet and start matching with the first character of sentence. This is all done until we find out that the sentence is pangram or not a pangram.


This article was intended to explain the logic of pangram in java code. If you want further explanation of the code or want to see its output in Android Studio. let me know. You can reach me via email or comment below.







Comments

Popular posts from this blog

Creat Your Own Android App In a Min

Camouflaged Merchants Of Death