1. Member
Join Date
Feb 2010
Posts
26
Rep Power
0

Swapping Characters

This is seriously driving me insane:
1) Ask the user to enter a word (this is the password, already figured this part out)
> Store it (already figured this part out).
> Encrypt it by asking the user to do 5 times the following:
>> Ask the user for a character position between 1 and the size of the word he/she entered (call this P, already did this part).
>> Ask the user for a number between 1 and the size of the word he/she entered (call this S, already did this part).
>> Swap the character at location (P-1)(charAt(P-1)) with the character at location (P-1+S)mod(size of the word -1)

^^ I HAVE NO IDEA WHAT TO DO!!!!!!

If someone could make some examples of what I need to do, that would be very helpful.

Here's what I have so far:
Last edited by besweeet; 02-17-2010 at 09:59 PM.

2. Member
Join Date
Dec 2009
Posts
24
Rep Power
0
you could make a char array like this:
char[] arr = pass.toCharArray();
then you could save the 2 chars that needs to be swapped in two temp char variables.
then insert the 2 chars in their new positions in the char array and then convert the char array to a String...
something like that..I hope you understand...maybe not the best solution, but i think it will work

3. Member
Join Date
Feb 2010
Posts
26
Rep Power
0
Originally Posted by wtd_nielsen
you could make a char array like this:
char[] arr = pass.toCharArray();
then you could save the 2 chars that needs to be swapped in two temp char variables.
then insert the 2 chars in their new positions in the char array and then convert the char array to a String...
something like that..I hope you understand...maybe not the best solution, but i think it will work

I don't understand it that much...

Here's what I have:

4. Member
Join Date
Dec 2009
Posts
24
Rep Power
0
take a look at this, try it:
int p = 2;
int s = 7;
char a = pass.charAt(p-1);
char b = pass.charAt((p-1+s)%pass.length());
char[] arr = pass.toCharArray();
arr[p-1] = b;
arr[(p-1+s)%pass.length()] = a;

System.out.println(arr);

5. Member
Join Date
Feb 2010
Posts
26
Rep Power
0
Originally Posted by wtd_nielsen
take a look at this, try it:
int p = 2;
int s = 7;
char a = pass.charAt(p-1);
char b = pass.charAt((p-1+s)%pass.length());
char[] arr = pass.toCharArray();
arr[p-1] = b;
arr[(p-1+s)%pass.length()] = a;

System.out.println(arr);
WOW! THANKS A LOT!

Now I also have to do this same exact process 4 more times. How would I go about doing that? It's in the same program. I have to "encrypt" a password a total of 5 times using this algorithm.

6. Member
Join Date
Dec 2009
Posts
24
Rep Power
0
well, you only need to convert the String password to a char array once..so something like this will work:

char[] arr = pass.toCharArray();
int counter = 0;
while(counter<5)
{

// save the 2 chars in temp variables
// swap the chars
counter++;
}

7. Member
Join Date
Dec 2009
Posts
24
Rep Power
0
use the toString method on the char array to convert the password to a String

8. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
8
StringBuffer could also be useful for this type of situation.

9. Member
Join Date
Feb 2010
Posts
26
Rep Power
0
Originally Posted by wtd_nielsen
well, you only need to convert the String password to a char array once..so something like this will work:

char[] arr = pass.toCharArray();
int counter = 0;
while(counter<5)
{

// save the 2 chars in temp variables
// swap the chars
counter++;
}
Hmm... Could you explain in more detail of what that chunk of code does?

We haven't learned this stuff yet. We were asked to use if/else/switch statements to swap the letters around, but I just can't understand how to do so.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•