Results 1 to 9 of 9
  1. #1
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default 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 08:59 PM.

  2. #2
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    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. #3
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by wtd_nielsen View Post
    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
    Thanks for the reply.

    I don't understand it that much...

    Here's what I have:

  4. #4
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    take a look at this, try it:
    String pass = "password";
    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. #5
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by wtd_nielsen View Post
    take a look at this, try it:
    String pass = "password";
    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. #6
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    well, you only need to convert the String password to a char array once..so something like this will work:

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

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

  7. #7
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    use the toString method on the char array to convert the password to a String

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    StringBuffer could also be useful for this type of situation.

  9. #9
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by wtd_nielsen View Post
    well, you only need to convert the String password to a char array once..so something like this will work:

    String pass = "password";
    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.

Similar Threads

  1. Swapping elements of an array help please
    By ikillu in forum New To Java
    Replies: 11
    Last Post: 01-15-2012, 08:49 PM
  2. swapping elements of vector
    By sara12345 in forum New To Java
    Replies: 1
    Last Post: 01-07-2010, 09:45 PM
  3. Binary Tree Traversals and Swapping
    By Sylar in forum New To Java
    Replies: 0
    Last Post: 03-08-2009, 07:52 AM
  4. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 08:56 PM
  5. Replies: 9
    Last Post: 07-10-2008, 04:52 PM

Posting Permissions

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