Skip to content

Instantly share code, notes, and snippets.

@FareedUllah09
Created April 19, 2019 17:53
Show Gist options
  • Select an option

  • Save FareedUllah09/35927d77d265e27d6a4c7d64f65a990e to your computer and use it in GitHub Desktop.

Select an option

Save FareedUllah09/35927d77d265e27d6a4c7d64f65a990e to your computer and use it in GitHub Desktop.
Q1. We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all
the bunnies recursively (without loops or multiplication).
import java.util.Scanner;
public class Q2 {
static int NumOfEars(int numOfBunnies) {
int num =0;
num = numOfBunnies + numOfBunnies;
return num;
}
public static void main(String[] args) {
int numOfBunnies = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the Number of Bunnies ");
numOfBunnies = scanner.nextInt();
int x = NumOfEars(numOfBunnies);
System.out.println("The Ears of the " + numOfBunnies + " Bunnies will be " + x);
}
}
Q2. Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances
of "not" anywhere in the string (case sensitive).
public class Task2 {
static boolean CheckTheOccurences(String word) {
String not = "not";
String is = "is";
int x = word.indexOf("not");
int occurenceOfNot =0;
while (x != -1) {
occurenceOfNot++;
x = word.indexOf("not" , x+1);
}
int occurenceOfIs =0 ;
int k = word.indexOf("is");
while (k != -1) {
occurenceOfIs = occurenceOfIs + 1 ;
k = word.indexOf("is" , k+1 );
}
return occurenceOfIs == occurenceOfNot;
}
public static void main(String[] args) {
String sentence = "noisxxnotyynotxis";
boolean x = CheckTheOccurences(sentence);
System.out.println(x);
}
}
@waqaskhan409
Copy link

Great Fareed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment