+ 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
2 Antworten
+ 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 ☺👍
+ 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