Find duplicate characters in a String and count the number of occurances using Java

前端 未结 30 2534
遇见更好的自我
遇见更好的自我 2020-12-14 11:47

How can I find the number of occurrences of a character in a string?

For example: The quick brown fox jumped over the lazy dog.

Some example

30条回答
  •  悲哀的现实
    2020-12-14 12:48

    import java.io.*;
    public class CountChar 
    {
    
        public static void main(String[] args) throws IOException
        {
          String ch;
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter the Statement:");
          ch=br.readLine();
          int count=0,len=0;
            do
            {  
              try
              {
              char name[]=ch.toCharArray();
                  len=name.length;
                  count=0;
                  for(int j=0;j=65&&name[0]<=91)||(name[0]>=97&&name[0]<=123))) 
                          count++;
                   }
                  if(count!=0)
                    System.out.println(name[0]+" "+count+" Times");
                  ch=ch.replace(""+name[0],"");          
              }
              catch(Exception ex){}
            }
            while(len!=1);
       }
    
    }
    

    Output

    Enter the Statement:asdf23123sfsdf
    
    a 1 Times
    
    s 3 Times
    
    d 2 Times
    
    f 3 Times
    

提交回复
热议问题