The quick brown fox jump over the lazy dog

The Quick Brown Fox

One day, I read, that this sentence “the quick brown fox jump over the lazy dog” contains all the character of the alphabet,  just because I’m so curious,  I thought to create a simple java application that checks whether the sentence is actually using all the letters in the alphabet or not.

The pseudo code of the application will look like this:

  1. Declare the string.
  2. Remove all spaces between the words from the sentence.
  3. Remove duplicate character.
  4. Convert the string into Character Arrays.
  5. Sort the Character Arrays.
  6. Loop the Character Array and print out to console.

First, I need to create a function that can remove duplicate character from a given parameter and the return of the function have to be a string. Ok, here’s the code:

    public static String removeDuplicates(String s) {
        StringBuilder noDupes = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            String si = s.substring(i, i + 1);
            if (noDupes.indexOf(si) == -1) {
                noDupes.append(si);
            }
        }
        return noDupes.toString();
    }

The function above using StringBuilder object as a container to process removing duplicate character, and the function will return the string formed by StringBuilder object. And then, I will use this function in the application. Ok, here’s the code:

import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: Josescalia
 * Date: 7/8/11
 * Time: 8:24 PM
 * To change this template use File | Settings | File Templates.
 */
public class ArrayTest {
    public static void main(String[] args) {
        //the string declaration
        String sStr = "the quick brown fox jumps over the lazy dog";
        //remove the spaces
        sStr = sStr.replace(" ", "");
        //remove the duplicate character using the function
        sStr = removeDuplicates(sStr);
        //Convert string into Character Array
        char[] a = sStr.toCharArray();
        //Sort the Array
        Arrays.sort(a);
        //let's loop and print out...is is correct, using all the character of the alphabet ????
        for (int i = 0; i < a.length; i++) {
            char c = a[i];
            System.out.print(c);
        }
        //it's true :P
    }

    public static String removeDuplicates(String s) {
        StringBuilder noDupes = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            String si = s.substring(i, i + 1);
            if (noDupes.indexOf(si) == -1) {
                noDupes.append(si);
            }
        }
        return noDupes.toString();
    }
}

Well, that’s all. Feel free to use and explore for your creations and needs.

I Hope that’s usefull

Thanks

Josescalia


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s