Java program to find a total number of characters from a given sentence.

Java program to find a total number of characters from a given sentence.


import java.io.*;
public class CharacterFinder
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        char ch,ch1;
        int i,l,c=0;
        String st;
        System.out.print("Enter String :");
        st=br.readLine();
        l=st.length();
        System.out.print("Enter a Character :");
        ch1=(char)br.read();
        for(i=0;i<l;i++)
        {
            ch=st.charAt(i);
            if(ch==ch1)
             c++;
        }
        System.out.println("Total :"+c);
    }
}

Output
Enter String: The quick brown fox
Enter a Character: q
Total: 1
Enter String: He is a good boy. He is doing a good job.
Enter a Character :o
Total: 7

No comments:

Post a Comment