In: Computer Science
3. Consider the string:”34456754”. Please find the frequency of each number. With a java program.
In below code, we need to find frequency of each digit in the string:
for this we have declared one variable called digit , which will iterate fro 0 to 9 using while loop
Inside while loop, for loop is defined , which will run number of times of string's length , and number of digit frequency will be stored in variable flag
Further, if flag is greater than 0. that means if number will be in string for atleast one time, the result will be displayed in console using If statement
____________________________________________
Please find the code in JAVA:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
System.out.println("Enter the
string of digits");
String str=scr.nextLine();
int flag=0,digit=0;
char value='0';
while(digit<=9){
flag=0;
for(int
i=0;i<str.length();i++){
if(String.valueOf(digit).equals(String.valueOf(str.charAt(i)))){
flag+=1;
value=str.charAt(i);
}
}
if(flag>0){
System.out.println(value+" in
string appears "+ flag +" times.");
}
digit++;
}
}
}
Please find the OUTPUT screenshot: