0

I typed out this program to count the number of times each character appears in a String.

  import java.util.Scanner;
  public class fre {


public static void main(String[] args) {
    Scanner s=new Scanner (System.in);
    System.out.println("Enter a string");
    String sent = s.nextLine();
    String str=sent.toUpperCase();
    int len=str.length();
    char save[]=new char[len];
    for (int i=0;i<len;i++){

        save[i]=str.charAt(i);


    }
    char a=0;
    int count=0;
    for(int i=0;i<len;i++){

        a=save[i];
        for(int j=0;j<len;j++){
            if(save[j]==a)
                count ++;
            }

            }
    for(int i=0;i<len;i++)
    System.out.println(save[i]+" appears "+count+" number of times");
}

}

The code is horribly wrong, can someone please guide me as to how to go about the program using simple functions and tell me what I've done wrong here?

Ahmed
  • 2,176
  • 5
  • 26
  • 40
Nikhil Gopal
  • 107
  • 3
  • 6
  • 11
  • Are you differentiating between uppercase and lowercase? – C.B. Jan 16 '14 at 17:07
  • For what it's worth, if you want to get an array of `char`s from a `String`, no need to do the loop yourself; there's already a `toCharArray` method. – Dennis Meng Jan 16 '14 at 19:19

6 Answers6

2

You can do it simply by

  • Declaring an array of 26 integers (1st index refers to A, second to B and so on)
  • Just traverse the input string once and for each character you traverse in string, increment corresponding index, you can do it simply like int index=inputString[i]-65; and increment this index.
  • Now traverse your array for the counts of each character and you are done, Hope it helps
Ahmed
  • 2,176
  • 5
  • 26
  • 40
1

How about

  int[] count = new int[256];
  for(int ch; (ch = System.in.read()) >= ' ';)
      count[ch]++;
  for(char ch = 0; ch < count.length; ch++)
      if (count[ch] > 0)
          System.out.println(ch + " appears " + count[ch] + " times");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You have one global count while you need one counter for each character.

Try using a Map<Character, Integer> to store the number of occurences of each character in your string.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

You need a separate count for each character. Right now you are incrementing the same count.

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
0

If i understand what you want is something like this:

    int count = StringUtils.countMatches("Auto-generated method stub", "e");
    System.out.println(count);

StringUtils is a api from apache http://commons.apache.org/proper/commons-lang/

xild
  • 187
  • 8
0

You can also use Collections.frequency(Collection<?> c, Object o) method to get count of an element in a collection, So below code snippet will give you the character count in a string.

String str=s.nextLine().toUpperCase();
         char[] letters=str.toCharArray();
         List<Character> cList = new ArrayList<Character>();
         for(char c : letters) {
             cList.add(c);
         }
         Set<Character> chSet=new HashSet<Character>(cList); //to get unique characters in the list
         for(Character ch:chSet) {
             System.out.println(ch+"          "+Collections.frequency(cList, ch));
         }
gowtham
  • 977
  • 7
  • 15