DEV Community

Discussion on: Daily Challenge #166 - Cat and Mouse

Collapse
 
mathanagopal97 profile image
Mathanagopal Sankarasubramanian • Edited

JAVA

import java.util.Scanner;

public class CatAndMouse
{
   public static void main (String[]args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the string x");
      String x = sc.nextLine();
      System.out.println("Enter the jump");
      int j = sc.nextInt();
      x = x.toLowerCase();
      int posofc = x.indexOf('c') + 1, posofm = x.indexOf('m') + 1, posofdog = x.indexOf('d');
      if(posofdog>0){//Check if dog is in between
         int start = posofc > posofm ? posofm : posofc;
         int end = posofc > posofm ? posofc : posofm;   
         if(start<posofdog && end>posofdog){
            System.out.println("Protected");
            System.exit(0);
         }
      }
      int compare = posofc < posofm ? (posofm - posofc - 1) : (posofc - posofm - 1);
      if (compare > j){
         System.out.println ("Escape");
      }
      else{
         System.out.println ("Caught");
      }

   }
}