Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count specific characters in a string (Java)

I have a homework assignment to count specific chars in string.

For example: string = "America"

The output should be = a appear 2 times, m appear 1 time, e appear 1 time, r appear 1 time, i appear 1 time and c appear 1 time

public class switchbobo {

/**
 * @param args
 */     // TODO Auto-generated method stub
  public static void main(String[] args){
    String s = "BUNANA";
    String lower = s.toLowerCase();
    char[] c = lower.toCharArray(); // converting to a char array
    int freq =0, freq2 = 0,freq3 = 0,freq4=0,freq5 = 0;

    for(int i = 0; i< c.length;i++) {
        if(c[i]=='a') // looking for 'a' only
          freq++;
        if(c[i]=='b')
          freq2++;
        if (c[i]=='c') {
          freq3++;
        }

        if (c[i]=='d') {
          freq4++;
        }       
    }
    System.out.println("Total chars "+c.length);
    if (freq > 0) {
      System.out.println("Number of 'a' are "+freq);
    }
  }
}

code above is what I have done, but I think it is not make sense to have 26 variables (one for each letter). Do you guys have alternative result?

like image 755
user1432513 Avatar asked Jun 03 '12 00:06

user1432513


People also ask

How do I count a specific character in Java?

To count characters in a string in Java, there are different methods: using for loop, charAt() method, String. chars. count() method, and the String. length() method.

How do you count occurrences of characters in a string in Java?

In order to find occurence of each character in a string we can use Map utility of Java.In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before.

How do you count specific occurrences of character in a string?

The string count() method returns the number of occurrences of a substring in the given string. In simple words, count() method searches the substring in the given string and returns how many times the substring is present in it.


2 Answers

Obviously your intuition of having a variable for each letter is correct.

The problem is that you don't have any automated way to do the same work on different variables, you don't have any trivial syntax which helps you doing the same work (counting a single char frequency) for 26 different variables.

So what could you do? I'll hint you toward two solutions:

  • you can use an array (but you will have to find a way to map character a-z to indices 0-25, which is somehow trivial is you reason about ASCII encoding)
  • you can use a HashMap<Character, Integer> which is an associative container that, in this situation, allows you to have numbers mapped to specific characters so it perfectly fits your needs
like image 52
Jack Avatar answered Sep 19 '22 13:09

Jack


You can use HashMap of Character key and Integer value.

HashMap<Character,Integer> 

iterate through the string

-if the character exists in the map get the Integer value and increment it.
-if not then insert it to map and set the integer value for 0

This is a pseudo code and you have to try coding it

like image 38
Sleiman Jneidi Avatar answered Sep 23 '22 13:09

Sleiman Jneidi