The quick brown fox jump over the lazy dog

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:
- Declare the string.
- Remove all spaces between the words from the sentence.
- Remove duplicate character.
- Convert the string into Character Arrays.
- Sort the Character Arrays.
- 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
No comments yet.
