Question

Print frequency of Consonants in a given sentence using recursion

				
					ENTER THE SENTENCE
the rain stopped
FREQUENCY OF CONSONANTS IN the rain stopped IS 9
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class frequencyConsonantsInSentence
{
    char ch=' ';
    public int FrequencyOfConsonants(String sen,int i,int count)
    {

        if(i< sen.length())
        {
            ch=sen.charAt(i);
            if(Character.isLetter(ch)==true)
            {
                if(ch!='a'&& ch!='e'&& ch!='i'&& ch!='o'&& ch!='u')
                {
                    count++;
                }

            }

            return FrequencyOfConsonants(sen,i+1,count);
        }
        else
        {
            return count;  
        }
    }

    public static void main()
    {
        String sen="";
        Scanner sc=new Scanner(System.in);
        frequencyConsonantsInSentence ob1=new frequencyConsonantsInSentence();
        System.out.println("ENTER THE SENTENCE");
        sen=sc.nextLine();
        sen=sen.toLowerCase();
        System.out.print("FREQUENCY OF CONSONANTS IN "+sen+" IS "+ob1.FrequencyOfConsonants(sen,0,0));

    }

}
				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *