Find out count of all characters in string (including special characters) in java


In this core java programming tutorial we will write a program to Find out count of all characters in string (including special characters) in java.



Write a program to find out Find out count of all characters in string, special characters also must be counted in java.

Example in java>
inputString is : This is it
Output is : T=1 h=1 i=3 s=2  =2 t=1

Must read: Reverse String using recursion in java.  

Full Program/SourceCode/ Example to Find out count of all characters in string (including special characters) in java >
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
public class  CharacterCountInStringExample {
  
   public static void main(String[] args) {
      String inputString="This is it";
       System.out.println("inputString is : "+inputString);
       System.out.print("Output is : ");
       characterCount(inputString);
   }
  
   /**
    * Method calculates count of all characters in inputString.
    */
   public static void characterCount(String inputString){
     
      char[] inputAr=inputString.toCharArray();
      int count=0,arLength;
     
       arLength=inputAr.length;
       for(int x=0; x<arLength; x++){
        char ch=inputAr[x];
        for(int y=x+1; y<arLength; y++){
            if(inputAr[y]==ch){          //if we have find same character again in string
                for(int z=y; z< arLength-1; z++)   //shift characters left.
                inputAr[z]=inputAr[z+1];
                arLength--;   //as characters have been reduce arLength.
                y=x;   //done to tackle case if occurrence of character is more than once in string.
            }
        }
       }
      
       /*
     * Till this point of program, inputAr's first arLength number of elements are unique.
     */
      
       for(int x=0;x<arLength;x++){
          count=0;
        for(int y=0; y<inputAr.length; y++){
            if(inputAr[x]==inputString.charAt(y))
            count++;
        }
        System.out.print(inputAr[x]+"="+count+" ");
       }
      
      
   }
  
}
/*OUTPUT
inputString is : This is it
Output is :T=1 h=1 i=3 s=2  =2 t=1
*/


So in this core java programming tutorial we wrote a program how to Find out count of all characters in string (including special characters) in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



Previous program                                                                  Next program




RELATED LINKS>



No comments:

Post a Comment