954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Swap method

How do you write a method that switch a space " _ " with a letter, the example my teacher gave us to work with is:

public static int seqSwapSearch ( int[] list, int item)
{
int location = -1;
int temp;
for (int i = 0; i < list.length; i++)
{
if (list[i] == item)
{
location = i;
if (i > 0) // can only swap if we are not at first element
{
temp = list[i-1]; // store the previous element
list[i-1] = list[i]; // overwrite the previous element
list[i] = temp; // overwrite the current element
}
}
}
return location;
}


but I have to use this method for a string all i could come up with is:

public static String letterSwap()
  {
    String s = secretWord();
    char [] String;
    char temp;
    temp = String[s.charAt(letterGuessing())];
    String[s.charAt(letterGuessing())] = String[letterGuessing()];
  }
saveme123
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

provide some sample input and output
for your program

code learner
Newbie Poster
8 posts since Jan 2011
Reputation Points: 11
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: