Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>>
Java
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);
            //res=res+strList.substring(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

Java
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
Java
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);	
   }
}
Posted
Updated 13-Sep-14 8:42am
v5

OK - the first thing to note is that you will run off the end of the string:
Java
for(int i = 0; i < currentString.length(); i++){

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

If you have 1 character in the string, then the loop will be entered, and i will be zero. Which is fine, if you didn't try to use i + 1 as well...
So you need to change the termination condition of your loop.

Other than that - what have you noticed that is wrong with it?
 
Share this answer
 
I appreciate your resourcefulness in using all available resources to solve your problem.
However, asking others to do your assignments for you isn't going to make you a better programmer.

I recommend spending some time in the debugger analysing the problems with your existing code and modifying it accordingly. It may take longer, but it'll be worth it...
 
Share this answer
 
Comments
Nelek 13-Sep-14 14:43pm    
That would fit better as a comment than a solution, don't you think?
the question is .. in the array will be more than one (Char) and what i want is Scramble just letter A inside it >>
 
Share this answer
 
i tried but it dose not work can anyone help me with that pleas

Java
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);	
		}
 
Share this answer
 
Comments
Nelek 13-Sep-14 14:29pm    
Please don't post solutions to chat with people asking or answering. The messages are not always sorted by date, so it can be a bit difficult to follow them correctly.
The best option is to use the "Have a question or comment?" (or the tiny "reply" on another comment). Another advantage is, that the person you write to will get a notification, otherwise it could be that he/she doesn't see your additional message.

If you need to add or change content to your question or a previous solution, please use the "improve question / solution" instead of writing a lot of messages. It will keep readability and help other users with similar problems to find it faster.
Richard MacCutchan 13-Sep-14 18:37pm    
letters[i]+=letters[i+1];
The += operator adds the second letter to the first creating some strange character. Also it seems you deleted my earlier suggestions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900