Write a program where the user enters a word and it counts t

Write a program where the user enters a word and it counts the number lowercased of a\'s, b\'s, and c\'s in the word. Example: Enter a word abracadabra In the word abracadabra there are 5 a\'s 2 b\'s 1 c\'s public static void main (String [] args)(Scanner keyboard = new Scanner (System.in);//Put your code here

Solution

import java.util.Scanner;

public class Occurence {
public static void main(String args[])
{
int count=0;
System.out.println(\"Enter the String: \");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for(char i=\'a\';i<=\'z\';i++)
{
for(int j=0;j<s.length();j++)
{
if(s.charAt(j)==i){
count++;
}
}
if(count!=0){
System.out.println(i+\"=\"+count);
count=0;
}
}
}}

Output:- Enter the string:- abracadabra

a=5

b=2

c=1

d=1

r=2

Explanation:-

As, i loop is initializesd from \'a\' to \'z\' and j loop is initialised from 0 to length of string. charAt() method is used to extract the character at specified position. The next condition is used to check that character of string is amtched with i loop or not. If, it is then value of count is incremented and value of i and count is printed and again count is initialised to zero for next iteration.

 Write a program where the user enters a word and it counts the number lowercased of a\'s, b\'s, and c\'s in the word. Example: Enter a word abracadabra In the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site