In this program we will see that how can we get the occurrence of the specific character given by user.here i have given 2 method to do same operation.click on read more to see.
class CountSpecificCharacter
{
public static
void main(String[] args)
{
String
str="Helloo Woorld!";
char ch='l';
//method#1
int count1=0;
for(int
i=0;i<str.length();i++)
{
if(str.charAt(i)==ch) count1++;
}
System.out.println("from method 1::"+count1);
//method#2
int count2=0;
while(str.indexOf(ch)>=0)
{
count2++;
str=str.substring(str.indexOf(ch)+1);
}
System.out.println("from method 2::"+count2);
}
}
|
Output: