top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java method which will take input as a string and return a string with char count?

+2 votes
465 views

I want a java method which will take input as a string and return a string with char count, please help.

posted Apr 19, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Try this code

HashMap<Character,Integer> map = new HashMap<Character,Integer>();          
String s = "aasjjikkk";
for(int i = 0; i < s.length(); i++)
{
   char c = s.charAt(i);
   Integer val = map.get(new Character(c));
   if(val != null)
   {
     map.put(c, new Integer(val + 1));
   }
   else
   {
     map.put(c,1);
   }
}
answer Apr 22, 2017 by Azhar Keer
Similar Questions
+3 votes

Implement a code that counts the occurrences of each word in an input file and write the word along with corresponding count in an output file sorted by the words alphabetically.

Sample Input

Gaurav is  a Good Boy
Sita is a Good Girl
Tommy is  a Good Dog
Ram is a Good person

Sample Output

D:\>Java FileWordCount inputFile.txt outputFile.txt
    Boy : 1
    Dog : 1
    Gaurav : 1
    Girl : 1
    Good : 4
    Ram : 1
    Sita : 1
    Tommy : 1
    a : 4
    is : 4
    person : 1
+2 votes

Given a string and a non empty word string, return a string made of each char just before and just after every appearance the word in the string.
Ignore cases where there is no char before or after the word, and a char may be included twice if it is between two words.

If inputs are abcXY123XYijk and XY output should be c123

+3 votes

Given input as a string, return an integer as the sum of all numbers found in the string

Input: xyzonexyztwothreeeabrminusseven
Output : 1 + 23 + (-7) = 17

+2 votes

Weight of vowels that appear in the string should be ignored and All non-alphabetic characters in the string should be ignored.

Weight of each letter is its position in the English alphabet system, i.e. weight of a=1, weight of b=2, weight of c=3, weight of d=4, and so on….weight of y=25, weight of z=26
Weight of Upper-Case and Lower-Case letters should be taken as the same, i.e. weight of A=a=1, weight of B=b=2, weight of C=c=3, and so on…weight of Z=z=26.

Example1:
Let us assume the word is “Hello World!!”
Weight of “Hello World!!” = 8+0+12+12+0+0+23+0+18+12+4+0+0 = 89
Note that weight of vowels is ignored. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.

...