How to count number of 't' in string in java

//How to count number of 't' in string in java


import java.util.*;
public class M
{
public static void main(String args[])
{
String str;
int count=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string:");
str=sc.nextLine();
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='t')
{
count++;
}
}
System.out.println("Total count: "+count);
}
}


OUTPUT:
Enter the string:
this is a table.
Total count: 2

No comments:

Post a Comment