+ 3

How to make a program which counts the number of letters in a String in Java?

tried to make one but couldn't, it's getting a bit frustrating

28th Jan 2018, 10:47 AM
Daniyal Hayat
2 Answers
+ 14
hint ::: extract every letter of String & see whether its come in range of char values or ascii values of alphabets ... if it comes then increase value of some int variable by 1 in each run of loop //print that int variable â˜ș👍
28th Jan 2018, 10:51 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 9
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); //may by this approach you can do that
28th Jan 2018, 11:04 AM
GAWEN STEASY
GAWEN STEASY - avatar