how to count selected Character from a sentence in java


Output:

C contents  3 times.

DuplicateCharacter.java

package com.swain.cell;

import java.util.HashMap;

public class DuplicateCharacter {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub

        String str = "Character duplicate word example java";
        char selectedchar='C';
        charch1[]=str.toUpperCase().toCharArray();
        HashMap<Character,Integer> hm=newHashMap<Character,Integer>();
        for(int i=0; i<ch1.length; i++)
        {
            char ch=ch1[i];
            hm.put(ch,(hm.get(ch)==null?1:hm.get(ch)+1));
        }
      
        System.out.println(selectedchar+" contents  " + hm.get(selectedchar)+ " times.");
       
       
   
       }

}


// amazon ebay shopbob.com hotels.com canon newegg.com 

Comments

Popular posts from this blog

difference between unset and unlink in php

htaccess dynamic url rewrite

Attempting to understand handling regular expressions with php