Write the method scrambleWord, which takes a given word and returns a string that contains a scrambled version of the word according to the following rules.
a. The scrambling process begins at the first letter of the word and continues from left to right.
b. If two consecutive letters consist of an "A" followed by a letter that is not an "A", then the two letters are swapped in the resulting string.
c. Once the letters in two adjacent positions have been swapped, neither of those two positions can be involved in a future swap.
The following table shows several examples of words and their scrambled versions.
word Result returned by scrambleWord(word)
"TAN" "TNA"
"ABRACADABRA" "BARCADABARA"
"WHOA" "WHOA"
"AARDVARK" "ARADVRAK"
"EGGS" "EGGS"
"A" "A"
"" ""

Respuesta :

Answer:

Let's implement the program using JAVA code.

import java.util.*;

import java.util.ArrayList;

import java.io.*;

public class ScrambledStrings

{

/** Scrambles a given word.

* atparam word the word to be scrambled

* atreturn the scrambled word (possibly equal to word)

* Precondition: word is either an empty string or contains only uppercase letters.

* Postcondition: the string returned was created from word as follows:

* - the word was scrambled, beginning at the first letter and continuing from left to right

* - two consecutive letters consisting of "A" followed by a letter that was not "A" were swapped

* - letters were swapped at most once

*/

public static String scrambleWord(String word)

{

// iterating through each character

for(int i=0;i<word.length()-1;)

{

//if the condition matches

if(word.charAt(i)=='A' && word.charAt(i+1)!='A'){

//swapping the characters

word=word.substring(0,i)+word.charAt(i+1)+word.charAt(i)+word.substring(i+2);

//skipping the letter

i+=1;

}

//skipping the letter

i+=1;

}

return word;

}

Explanation:

Note: Please replace the "at" on the third and fourth line with at symbol that is shift 2.