954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Character Frequency

I am trying to write a code to count the frequency of characters in a String;

For example if I have String str = new String("This is a test");

I want to count the number of occurrence of each character in the string.(assuming the code is case insensitive)

This is what I have at present ...

//


public static void main(String[] args) 
{
// TODO Auto-generated method stub

String temp = new String("This is a test message");
//int[] alphabets = new int [26];
int [] temp1 = new int[100];
char [] s = temp.toUpperCase().toCharArray();


for(int i=0;i<s.length;i++)
{
temp1[(s-'A')]++;
}

      for(int i =0;i<temp1.length;i++)
      {
         System.out.println(temp1);
       }

}

}

}

I need help on how to complete this implementation.

Thanks in advance for your help.

crestaldin
Light Poster
30 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

Yeah, we're not suppose to do your homework for you bud.

Do you have to use arrays? It might be best to use a HashMap here. Using a HashMap will allow you to count only the characters that you have and nothing else. However, you will have to make a lot of objects to make it work.

You also might want to look at the String methods .toUpperCase() and .toLowerCase().

hooknc
Posting Whiz in Training
219 posts since Aug 2005
Reputation Points: 11
Solved Threads: 8
 

Yeah, we're not suppose to do your homework for you bud.

Do you have to use arrays? It might be best to use a HashMap here. Using a HashMap will allow you to count only the characters that you have and nothing else. However, you will have to make a lot of objects to make it work.

You also might want to look at the String methods .toUpperCase() and .toLowerCase().

Thank you for the suggestion but ... that's actually not my homework. I just went online ( http://cs.furman.edu/ccscse2002/program_contest/problems/progcont_1995.pdf ) to get a website with Java problems and I'm working on them to improve on my skills.

crestaldin
Light Poster
30 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 
HashMap freqMap = new HashMap();
 
//FOR loop through chars goes here
if (freqMap.containsKey(character))
    freqMap.put(character, freqMap.get(character)+1);
else
    freqMap.put(character, 1);


Don't expect that code to work with a copy n paste job, you'll still need to do some casting and boxing.

You can even condense that 4-line IF statement into a single line using the alternative IF structure. Once you've solved this problem successfully, I'll show you how if interested.

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

Try this..

public void getFrequencyInString(){
String str = "apple";
HashMap hm = new HashMap();
for(int i = 0; i

Vijay1214
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You