Java program to count the number of holes in a given string
KW.java
Some of the examples are:
8 has 2 holes
W has no holes
# has 1 hole
% has 2 holes
合作 is non-ASCII char
——————————————————————————————————
importjava.util.Scanner;classKW{publicstaticvoidmain(Stringargs[]){System.out.println("———————————————————————————————————————————");System.out.println("Program to find number of holes in a string");System.out.println("———————————————————————————————————————————");char[]singlehole={'a','A','b','d','D','e','g','o','O','p','P','q','Q','R','0','4','6','9','@','#'};char[]doublehole={'B','8','&','%'};Strings="";Scannersc=newScanner(System.in);System.out.print("Enter any string ");s=sc.nextLine();s=s.replaceAll("[^\\p{ASCII}]","");System.out.print("\nKeyboard characters "+s);intcount=0;char[]ch=s.toCharArray();for(inti=0;i<ch.length;i++){for(intj=0;j<singlehole.length;j++){if(ch[i]==singlehole[j]){count+=1;}}for(intj=0;j<doublehole.length;j++){if(ch[i]==doublehole[j]){count+=2;}}}System.out.println("\n\nNumber of holes in keyboard characters "+count);System.out.println("———————————————————————————————————————————");}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
———————————————————————————————————————————
Program to find number of holes in a string
———————————————————————————————————————————
Enter any string KODINGWINDOW
Keyboard characters KODINGWINDOW
Number of holes in keyboard characters 4
———————————————————————————————————————————
kodingwindow@kw:~$ java KW
———————————————————————————————————————————
Program to find number of holes in a string
———————————————————————————————————————————
Enter any string 编码合作WINDOW
Keyboard characters WINDOW
Number of holes in keyboard characters 2
———————————————————————————————————————————
kodingwindow@kw:~$
//Removed non-ASCII characters
Java program to count the number of holes in a given number
File: KW.java (count holes in a number)
importjava.util.Scanner;classKW{publicstaticvoidmain(Stringargs[]){System.out.println("———————————————————————————————————————————");System.out.println("Program to find number of holes in a number");System.out.println("———————————————————————————————————————————");Strings="";Scannersc=newScanner(System.in);System.out.print("Enter any number ");s=sc.nextLine();intcount=0,holes=0;for(inti=0;i<s.length();i++){if(s.charAt(i)==('0')||s.charAt(i)==('4')||s.charAt(i)==('6')||s.charAt(i)==('9')){count++;holes=1;System.out.println("Hole in "+s.charAt(i)+" - "+holes);}elseif(s.charAt(i)==('8')){count+=2;holes=2;System.out.println("Holes in "+s.charAt(i)+" - "+holes);}else{holes=0;System.out.println("Hole in "+s.charAt(i)+" - "+holes);}}System.out.println("\nTotal number of holes in given number is "+count);System.out.println("———————————————————————————————————————————");}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
———————————————————————————————————————————
Program to find number of holes in a number
———————————————————————————————————————————
Enter any number 8789522453
Holes in 8 - 2
Hole in 7 - 0
Holes in 8 - 2
Hole in 9 - 1
Hole in 5 - 0
Hole in 2 - 0
Hole in 2 - 0
Hole in 4 - 1
Hole in 5 - 0
Hole in 3 - 0
Total number of holes in given number is 6
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?