To find the number of occurrence of a character in any string ( Java program)

java programming for find the occurrence of any char in any string


Program: 


 import java.util.*;
public class eval
{  
    public static void main(String arr[])
       {
          Scanner sc=new Scanner(System.in);
          System.out.println("enter any string");    
          String s1=sc.nextLine();
          int x=s1.length();
          System.out.println("length of string :  "+x);
          int i,j=0;
          System.out.println("enter any char to be searched");
          String ss=sc.next(); 
          char[] chh=ss.toCharArray(); 
          for(i=0;i<x;i++)
            {
                  if(s1.charAt(i)==ss.charAt(0))
                   {
                       j++;
                   } 
            }
          if(j==0)
             { 
                   System.out.println(" Char not found  ");
             } 
          if(j>0)
              {
                   System.out.println(" Char  found at  : "+j);
              }

      }
}


output:


Comments