Welcome, Guest. Please login or register.
Did you miss your activation email?
dotNETprogrammers.net/forum :: A community for .NET developers « Javaforums.net java programming boards « General Java related discussions «  (Moderator: jurka)Topic: Count the frequency of the same characters in a string
Pages: [1]   Go Down
  Print  
Author Topic: Count the frequency of the same characters in a string  (Read 927 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: July 19, 2008, 05:57:24 PM »

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:
Code
GeSHi (java):
  1. import java.util.ArrayList;
  2. import java.util.Vector;
  3. public class StringCount {
  4. public static void main(String[] args) {
  5. StringCount SC = new StringCount();
  6. SC.usage();
  7. 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.");
  8. }
  9. public void countCharacters(String input){
  10. String[] alphabet = {"a","b","c","d","e","f","g","h","i", "j", "k", "l", "m",
  11. "n","o", "p", "q", "r", "s", "t","u","v","w","x","y","z", " ", "[","]","'",",", "/","\\",":",".","-","(",")"};
  12. System.out.println("Input: " + input);
  13. System.out.println();
  14. ArrayList occurences = new ArrayList();
  15. int times = 0;
  16. for (int i = 0; i< alphabet.length; i++){
  17. for (int k = 0; k < input.length(); k++){
  18. if(alphabet[i].equals("" + input.toLowerCase().charAt(k))){
  19. times++;
  20. }
  21. }
  22. occurences.add(alphabet[i] +  " found\t" + times +  " times");
  23. times = 0;
  24. }
  25. for(int i = 0; i< occurences.size(); i++){
  26. System.out.println(occurences.get(i));
  27. }
  28. }
  29. public void usage(){
  30. System.out.println("By: HappyFace - www.engineeringserver.com :: v1.0 initial release");
  31. }
  32. }
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
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
dotNETprogrammers.net/forum :: A community for .NET developers « Javaforums.net java programming boards « General Java related discussions «  (Moderator: jurka)Topic: Count the frequency of the same characters in a string
 
Jump to:  

Your Ad Here