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.

Recommended Answers

All 6 Replies

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().

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.

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.

Try this..

public void getFrequencyInString(){
        String str = "apple";
        HashMap hm = new HashMap();
        for(int i = 0; i<str.length(); i++){
            char c = str.charAt(i);
            if(hm.get(c)!= null){
                hm.put(c, (Integer)(hm.get(c))+1);
            }else{
                hm.put(c, 1);               
            }
        }
        System.out.println(hm.entrySet());
    }

Output:

[e=1, p=2, a=1, l=1]

Try This..

import java.io.*;
public class frequency_of_char
{
    public static void main(String args[])throws IOException
    {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        int ci,i,j,k,l;l=0;
        String str,str1;
        char c,ch;
        System.out.println("Enter your String");
        str=in.readLine();
        i=str.length();
        for(c='A';c<='z';c++)
        {
            k=0;
            for(j=0;j<i;j++)
            {
                ch=str.charAt(j);
                if(ch==c)
                    k++;
            }
            if(k>0)
            System.out.println("The character "+c+" has occured for "+k+" times");
        }
    }
}

If you really must post an answer to a 6 year old thread, please make more of an effort to post decent code.
Starting a line 2 (ignores Java naming standards), continuing with line 4 (throws exception from main instead of handling it), to line 6 (a pile of cryptic eaningless variables), all the way to line 26 (and still not a single helpful comment) this is a truely terrible example of how to write a Java program.

You obviously know how to write code that works, so maybe a bit more attention to readability next time?

(Not to mention that we always try to teach people to develop the skills they need to solve their own progams, and never just post code for them to copy without understanding...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.