As salamo alaikum wa rahmatullah
package examples;
import java.util.Scanner;
public class CountOccurenceOfCharacterIntoString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String inputStr=sc.nextLine();
System.out.println("Enter a character which has to be counted into given string");
String cntStr=sc.nextLine();
int total_len=inputStr.length();
int len_inoccured_after_replace=inputStr.replace(cntStr, "").length();
int actual_len=total_len-len_inoccured_after_replace;
System.out.println("Count is"+actual_len);
}
}
Jazak Allah Khair
No comments:
Post a Comment