import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountSymbols {
/**
* @param args
*/
static void printsymbols(String inp,String sym){
int count=0;
Pattern p=Pattern.compile(sym);
Matcher m=p.matcher(inp);
while(m.find()){
count++;
}
System.out.println(sym+" has "+ count+" times");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
System.out.println("Enter the Symbol");
String sym=in.nextLine();
System.out.println("Enter the String");
String inp=in.nextLine();
CountSymbols.printsymbols(inp,sym);
}
}
Output:
Enter the Symbol
@
Enter the String
i@ the @ inp@ str@ing
@ has 4 times