Holes in a string

Count the holes
if D,O,P,Q,A have 1 hole
and B has 2 hole

input word: BABOY

output:6 holes

@cris18 traverse the string and do follows:

int main()
    {
    char st[100];
    scanf("%s",st);
    int l = strlen(st);
    for(int i=0; i<l; i++) {
    if(st[i] == 'A'||st[i] == 'D'||st[i] == 'P'||st[i] == 'O'||st[i] == 'Q')
      count += 1;
    if(st[i] == 'B')
       count += 2;
    }
    printf("%d Holes\n",count);
    return 0;
    }

Happy Coding!!!

2 Likes

Just timepass :wink:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int holes[26] = {1, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};

int main() {
    char s[100];
    cin>>s;
    int len = strlen(s);
    int hcount = 0;
    for(int i=0; i<len; i++) {
		hcount += holes[s[i] - 'A'];
    }
    cout<<hcount<<endl;
    return 0;
}

@cris18

Here is the Java solution you seem to need.

public static void main(String[] args) throws IOException {

    BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
    BufferedWriter out = new BufferedWriter( new OutputStreamWriter( System.out ) ):

    String s = in.readLine(); // Accepts the word as input.

    int count = 0;
    for( int i = 0; i < s.length(); i++ ) {

        // Checks if the character has one hole. R also has a hole.
        if( s.charAt( i ) == 'D' || s.charAt( i ) == 'O' || s.charAt( i ) == 'P' || s.charAt( i ) == 'Q' || s.charAt( i ) == 'A' |} s.charAt( i ) == 'R' ))
              count++;
        // Checks if the character has two holes.
        else if ( s.charAt( i ) == 'B' )
              count += 2;

    }

    out.println( count );
    out.flush();
    out.close();
}

This solution assumes that the word given as input will be one line. If it isn’t, a slight modification to the input acceptance will have to be made. This runs O(N) time where N is the length of the word.

import java.util.*;

class Hello

{

public static void main(String args[]){

System.out.print("Enter String : ");

Scanner sc=new Scanner(System.in);

String input=sc.next();

int holes=0;

for(int i=0;i<input.length();i++)

{

char ch=input.charAt(i);

if(ch==β€˜D’||ch==β€˜O’||ch==β€˜P’||ch==β€˜Q’||ch==β€˜A’)

holes++;

else if(ch==β€˜B’)

holes=holes+2;

}

System.out.println(" Holes in string β€œ+input+” are = "+holes);

}

}

DEMO -

Enter String : BABA

Holes in string BABA are = 6

/**
Title: HOLES IN THE TEXT
By: Chester C. Naguit
**/

import java.util.;
import java.text.
;
import java.io.*;

class HoleInTheText{
static void sop(Object o){System.out.print(o);}
static void sopln(Object o){System.out.println(o);}
static void sopf(String s, Object o){System.out.printf(s,o);}
static Scanner sc = new Scanner(System.in);

static int[] count = new int[40];
static String str[] = new String[100];
static int t=0;
public static void main(String args[]){ 
	boolean b = true;
	while(b){
	t = sc.nextInt();
	sc.nextLine();
		if(t>40){
			sopln("Out of Range! 40 no. of test only!");
		}else{
			b = false;
			Count(t);
		}
	}
}

static void Count(int t){
	boolean b = false;
	for(int s=0; s<t; s++){
		str[s] = sc.nextLine();
		char[] c = str[s].toCharArray(); // convert string into array of char
		for(int i=0; i<c.length; i++){
			if(c[i]=='P'||c[i]=='O'||c[i]=='A'||c[i]=='D'||c[i]=='R') count[s]++;
			else if(c[i]=='B') count[s]+=2;
		}
	}
	for(int i=0; i<t; i++){
		sopln(count[i]);
	}
}

}

import java.util.*;
public class Holes {

public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   String s;
   int holes=0;
   char c[]=new char[10];
   System.out.println("enter the given string to find holes");
   s=sc.next();
   c=s.toCharArray();
   for(int i=0;i<c.length;i++)
   {
	   if(c[i]=='A' || c[i]=='D' || c[i]=='P' || c[i]=='Q' || c[i]=='O')
		   holes++;
	   else if(c[i]=='B')
		   holes=holes+2;
	   else
		   holes=holes+0;
   }
   System.out.println("total no hoels is in your string is:"+holes);
}

}

R has no holes? :confused: R should have 1 hole as well. My code includes that.

can you help with this in java language?

can you help me with this in java language?

check the answers below, fellow users have done it! :slight_smile: