106k views
2 votes
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 the value "drag" to drWord. Submit

User Keneisha
by
6.0k points

2 Answers

2 votes

Answer:

String word = "George slew the dragon";

int pos = word.indexOf("dr");

String drWord = word.substring(pos, pos+4);

System.out.println(drWord);

Step-by-step explanation:

Assuming dr is always there, we don't have to check the validity of 'pos'. Normally, you would!

User Luu
by
6.5k points
3 votes

Answer:

Please see attachment

Step-by-step explanation:

Please see attachment

Assume that word is a variable of type String that has been assigned a value. Assume-example-1
User Stanekam
by
6.0k points