HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

Count number of each char in a String

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numbereachcharcountstring

Problem

I know there is a simpler way of doing this, but I just really can't think of it right now. Can you please help me out?

```
String sample = "hello world";
char arraysample[] = sample.toCharArray();
int length = sample.length();

//count the number of each letter in the string
int acount = 0;
int bcount = 0;
int ccount = 0;
int dcount = 0;
int ecount = 0;
int fcount = 0;
int gcount = 0;
int hcount = 0;
int icount = 0;
int jcount = 0;
int kcount = 0;
int lcount = 0;
int mcount = 0;
int ncount = 0;
int ocount = 0;
int pcount = 0;
int qcount = 0;
int rcount = 0;
int scount = 0;
int tcount = 0;
int ucount = 0;
int vcount = 0;
int wcount = 0;
int xcount = 0;
int ycount = 0;
int zcount = 0;

for(int i = 0; i < length; i++)
{
char c = arraysample[i];
switch (c)
{
case 'a':
acount++;
break;
case 'b':
bcount++;
break;
case 'c':
ccount++;
break;
case 'd':
dcount++;
break;
case 'e':
ecount++;
break;
case 'f':
fcount++;
break;
case 'g':
gcount++;
break;
case 'h':
hcount++;
break;
case 'i':
icount++;
break;
case 'j':
jcount++;
break;
case 'k':
kcount++;
break;
case 'l':
lcount++;
break;
case 'm':
mcount++;
break;
case 'n':
ncount++;
break;
case 'o':
ocount++;
break;
case 'p':
pcount++;
break;
case 'q':
qcount++;
break;
case 'r':
rcount++;
break;
case 's':
scount++;
break;
case 't':
tcount++;
break;
case 'u':
ucoun

Solution

Oh woah! xD It's just.. woah! What patience you have to write all those variables.

Well, it's Java so you can use a HashMap.

Write something like this:

String str = "Hello World";
int len = str.length();
Map numChars = new HashMap(Math.min(len, 26));

for (int i = 0; i < len; ++i)
{
    char charAt = str.charAt(i);

    if (!numChars.containsKey(charAt))
    {
        numChars.put(charAt, 1);
    }
    else
    {
        numChars.put(charAt, numChars.get(charAt) + 1);
    }
}

System.out.println(numChars);


  • We do a for loop over all the string's characters and save the current char in the charAt variable



  • We check if our HashMap already has a charAt key inside it



  • If it's true we will just get the current value and add one.. this means the string has already been found to have this char.



  • If it's false (i.e. we never found a char like this in the string), we add a key with value 1 because we found a new char



  • Stop! Our HashMap will contain all chars (keys) found and how many times it's repeated (values)!

Code Snippets

String str = "Hello World";
int len = str.length();
Map<Character, Integer> numChars = new HashMap<Character, Integer>(Math.min(len, 26));

for (int i = 0; i < len; ++i)
{
    char charAt = str.charAt(i);

    if (!numChars.containsKey(charAt))
    {
        numChars.put(charAt, 1);
    }
    else
    {
        numChars.put(charAt, numChars.get(charAt) + 1);
    }
}

System.out.println(numChars);

Context

StackExchange Code Review Q#44186, answer score: 35

Revisions (0)

No revisions yet.