Here's a demo I made that counts the frequency/occurences in a given string. Took me an hour or so to write it but it was fun to do so on a boring sunday. It also counts a few special character such as spaces and brackets etc.
Sourcecode:GeSHi (java):
import java.util.ArrayList;
import java.util.Vector;
public class StringCount {
public static void main
(String[] args
) { StringCount SC = new StringCount();
SC.usage();
SC.countCharacters("www.engineeringserver.com is a site for (student) software developers, especially Java developers that want to know more about Java and Java development in general.");
}
public void countCharacters
(String input
){ String[] alphabet =
{"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", " ", "[","]","'",",", "/","\\",":",".","-","(",")"};
System.
out.
println("Input: " + input
);
int times = 0;
for (int i = 0; i< alphabet.length; i++){
for (int k = 0; k < input.length(); k++){
if(alphabet[i].equals("" + input.toLowerCase().charAt(k))){
times++;
}
}
occurences.add(alphabet[i] + " found\t" + times + " times");
times = 0;
}
for(int i = 0; i< occurences.size(); i++){
System.
out.
println(occurences.
get(i
));
}
}
public void usage(){
System.
out.
println("By: HappyFace - www.engineeringserver.com :: v1.0 initial release");
}
}
Created by GeSHI 1.0.7.20
Output:By: HappyFace -
www.engineeringserver.com :: v1.0 initial release
Input:
www.engineeringserver.com is a site for (student) software developers, especially Java developers that want to know more about Java and Java development in general.
a found 14 times
b found 1 times
c found 2 times
d found 5 times
e found 22 times
f found 2 times
g found 3 times
h found 1 times
.. etc
This post can also be found on my blog here:
http://www.engineeringser...ters-in-a-string/#respond