Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that word is a variable of type String that has been assigned a value . A

ID: 3686599 • Letter: A

Question

Assume that word is a variable of type  String that has been assigned a value . Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip".

Assume that there is another variable  declared , drWord, also of type  String . Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord. So, if the value of word were "George slew the dragon" your code would assign thevalue "drag" to drWord.

in java please

Explanation / Answer

import java.util.Scanner;

public class substringDR {
  
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a string: ");
       String string = sc.nextLine();
      
       int index = string.indexOf("dr");
      
       String drWord = string.substring(index, index+4);
      
       System.out.println(drWord);
   }

}