This assignment involves reasoning about strings made up of uppercase letters. You will implement several static methods that appear in the same class (not shown). Here are the details.
1. The first method takes a single string parameter and returns a scrambled version of that string. The scrambling process begins at the first letter of the word and continues from left to right. 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. Once the letters in two adjacent positions have been swapped, neither of those two positions can be involved in a future swap.
public static String scrambleWord(String word)
The method takes a given word (an empty string or a string containing only upper case letters) and returns a string that contains a scrambled version of the word according to the rules given above. The following table shows several examples of words and their scrambled versions.
Original word After scrambling
"TAN" "TNA"
"ABRACADABRA" "BARCADABARA"
"WHOA" "WHOA"
"AARDVARK" "ARADVRAK"
"EGGS" "EGGS"
"A" "A"
"" ""
my code is >> i know it is wrong but pleas help me>>
package assignments;
import java.util.*;
import java.*;
public class ScrambleWord
{
public static void main(String[] args)
{
List<String> strList=new ArrayList<String>();
strList.add("TAN");
strList.add("ABRACADABRA");
strList.add("WHOA");
strList.add("EGGS");
strList.add("A");
strList.add("");
System.out.println(MainMethod.scrambleWordMeth(strList));
}
}
class MainMethod
{
public static String scrambleWordMeth(List<String> strList)
{
int curr=0;
String res="";
String currentString = strList.get(curr);
while (curr<strList.size())
{
for(int i = 0; i < currentString.length(); i++)
{
if (currentString.charAt(i) == 'A' && !(currentString.charAt(i+1)=='A'))
{
res=res+currentString.substring(curr+1,curr+2);
res=res+'A';
curr=curr+2;
}
else
{
res= res+currentString.substring(curr,curr+1);
curr++;
}
}
if (curr<strList.size())
{
res=res+currentString.charAt(curr);
}
}
return res;
}
}
additional information copied from non-solution 2
the question is .. in the array will be more than one (Char) and what i want is Scramble just letter A inside it >>
additional information copied from non-solution 5
i did it but i do not why it is still does not work
public static void main(String[] args)
{
List<string> strList=new ArrayList<string>();
strList.add("TAN");
strList.add("ABRACADABRA");
strList.add("WHOA");
strList.add("EGGS");
strList.add("A");
strList.add("");
System.out.print(MainMethod.scrambleAllwords(strList));
}
}
class MainMethod
{
public static void scrambleAllwords(List<string>strList)
{
for(int i=0;i<strlist.size();i++)
{
String scrabled=scrambleWordMeth(strList.get(i));
strList.set(i,scrabled);
}
}
public static String scrambleWordMeth(String strList)
{
char[] letters=strList.toCharArray();
int n=letters.length-1;
for(int i=0;i<n;i++)
{
char tmp= letters[i+1];
if (letters[i]=='A'&& !(letters[i+1]=='A'))
{
letters[i+1]=letters[i];
letters[i]=tmp;
}
return new String(letters);
}
return new String(letters);
}
}
additional information copied from non-solution 6
i tried but it dose not work can anyone help me with that pleas
public static String scrambleWordMeth(String strList)
{
char[] letters=strList.toCharArray();
int n=letters.length-1;
int i=0;
while(i<n)
{
char tmp= letters[i+1];
if (letters[i]=='A'&& !(letters[i+1]=='A'))
{
letters[i+1]=letters[i];
letters[i]=tmp;
}
else
{
letters[i]+=letters[i+1];
}
return new String(letters);
}
}