Program to count frequency of "yes" or "YES" in string.

Write a program to input a sentence. Count and print the occurrence/frequency of a word "yes" or "YES" from the String.



SOURCE CODE



import java.io.*;
import java.util.*;
public class YES_yes
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a String    :");
        String str=sc.nextLine();
        int k=0;
        str=str+" ";
        String s="";
        for(int i=0;i<str.length();i++)
        {
            char ch=str.charAt(i);
            if(ch!=' ')
            {
                s=s+ch;
            }
            else
            {
                 if(s.compareTo("yes")==0 || s.compareTo("YES")==0)
                {
                    k++;
                }
                s="";
            }
        }
        System.out.println("\n\n");
        System.out.println("'yes' has repeated "+k+" times");
    }
}





OUTPUT




If you have any question then leave a comment below I will do my best to answer that question.

Comments

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.