Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Write a program to count occurances of each character in a string

Write a program to count occurances of each character in a string

Scheduled Pinned Locked Moved Java
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chiku24
    wrote on last edited by
    #1

    suppose string is "mam is going"..so now finding each character occurances.....

    T R 2 Replies Last reply
    0
    • C chiku24

      suppose string is "mam is going"..so now finding each character occurances.....

      T Offline
      T Offline
      Tanvir Ahmed Tanin
      wrote on last edited by
      #2

      Here is the function where your get the occurrence of each character in any string with O(n) times.

      public static void main(String args[]){
      String str="mam is going";
      HashMap<String, Integer> freqOfChar=calculateCharFrequency(str!=null?str.trim():"");
      printFreeOfChar(freqOfChar);
      }

      static HashMap<String, Integer> calculateCharFrequency(String str){
      	HashMap<String, Integer> freqOfChar = new HashMap<String, Integer>();
      	String ch;
      	if(str!=null){
      		for(int i=0;i<str.length();i++){
      			ch=Character.toString(str.charAt(i)).trim();
      			if(ch.length()>0){
      				if(freqOfChar.get(ch)!=null){
      					freqOfChar.put(ch, (freqOfChar.get(ch)+1));
      				}else{
      					freqOfChar.put(ch, 1);
      				}
      			}
      		}
      	}
      	return freqOfChar;
      }
      
      static void printFreeOfChar(HashMap<String, Integer>  freqOfChar){
      	 for (String key : freqOfChar.keySet()) {
      		 System.out.println("Char: "+key+ "; Occurance: "+freqOfChar.get(key));
      	 }
      	
      }
      

      output:

      Char: o; Occurance: 1
      Char: i; Occurance: 2
      Char: m; Occurance: 2
      Char: a; Occurance: 1
      Char: n; Occurance: 1
      Char: g; Occurance: 2
      Char: s; Occurance: 1

      T 1 Reply Last reply
      0
      • T Tanvir Ahmed Tanin

        Here is the function where your get the occurrence of each character in any string with O(n) times.

        public static void main(String args[]){
        String str="mam is going";
        HashMap<String, Integer> freqOfChar=calculateCharFrequency(str!=null?str.trim():"");
        printFreeOfChar(freqOfChar);
        }

        static HashMap<String, Integer> calculateCharFrequency(String str){
        	HashMap<String, Integer> freqOfChar = new HashMap<String, Integer>();
        	String ch;
        	if(str!=null){
        		for(int i=0;i<str.length();i++){
        			ch=Character.toString(str.charAt(i)).trim();
        			if(ch.length()>0){
        				if(freqOfChar.get(ch)!=null){
        					freqOfChar.put(ch, (freqOfChar.get(ch)+1));
        				}else{
        					freqOfChar.put(ch, 1);
        				}
        			}
        		}
        	}
        	return freqOfChar;
        }
        
        static void printFreeOfChar(HashMap<String, Integer>  freqOfChar){
        	 for (String key : freqOfChar.keySet()) {
        		 System.out.println("Char: "+key+ "; Occurance: "+freqOfChar.get(key));
        	 }
        	
        }
        

        output:

        Char: o; Occurance: 1
        Char: i; Occurance: 2
        Char: m; Occurance: 2
        Char: a; Occurance: 1
        Char: n; Occurance: 1
        Char: g; Occurance: 2
        Char: s; Occurance: 1

        T Offline
        T Offline
        TorstenH
        wrote on last edited by
        #3

        Why do you do his homework? Does he pay you? Does he learn anything?

        regards Torsten I never finish anyth...

        1 Reply Last reply
        0
        • C chiku24

          suppose string is "mam is going"..so now finding each character occurances.....

          R Offline
          R Offline
          Raushank03
          wrote on last edited by
          #4

          The Answer is available on this link http://techgurulab.com/course/java-tutorials/[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups