Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:05:31+00:00 2026-06-03T22:05:31+00:00

I am manipulating ssn. Input =123456789 Output=896745123 You take the first 3 digits from

  • 0

I am manipulating ssn.
Input =”123456789″
Output=”896745123″

You take the first 3 digits from the input (i.e 123) and swap it with the last two digits “89” and then swap “45” with “67”. Below is the code I have using toCharArray. But I didn’t like it. This method (not the test) can be accessed by many users at a time. I was looking for a better in performance, readability and trade safe solution.

I appreciate your help.

@Test
    public void testSSNString(){

        String original="123456789";
        String result="896745123";
        char[] charResult=original.toCharArray();

        char temp=charResult[6];
        charResult[6]=charResult[0];
        charResult[0]=charResult[7];
        charResult[7]=charResult[1];
        charResult[1]=charResult[8];
        charResult[8]=charResult[2];
        charResult[2]=charResult[5];
        charResult[5]=charResult[4];
        charResult[4]=charResult[3];
        charResult[3]=temp;

        assertEquals(original.toCharArray(),charResult);

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:05:33+00:00Added an answer on June 3, 2026 at 10:05 pm

    The way that you have given is essentially the best you can do in Java in terms of performance. Using arrays is faster than using StringBuilder.

    To improve readability, I would just make some small changes, like making a new array for the permuted output:

    @Test
    public void testSSNString() {
      String original = "123456789";
      String expected = "896745123";
    
      char[] in = original.toCharArray();
      char[] out = new char[9];
      out[0] = in[7];
      out[1] = in[8];
      out[2] = in[5];
      out[3] = in[6];
      out[4] = in[3];
      out[5] = in[4];
      out[6] = in[0];
      out[7] = in[1];
      out[8] = in[2];
    
      assertEquals(expected.toCharArray(), out);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried manipulating fb_button_text value using jQuery but, it didn't work at all. Then
Regarding passing arrays to functions and then manipulating the array contents (Array.Reverse) and then
Windows Form Application – Manipulating input-elements in WinForm WebBrowser Although I am familiar with
I'm having trouble manipulating JavaScript generated html elements. Depending on user input - a
For two threads manipulating a container map for example, what the correct way to
I am having difficulties manipulating an input field, image and text all in the
When calculating and manipulating probabilities in Java, and then expressing them as percentages, what
I know that when manipulating UI controls from any non-UI thread, you must marshal
I'm testing performance for manipulating Bitmap pixels on the JNI layer using OpenCv. Two
I am dealing uploading large .jpg image and then manipulating it through variables. At

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.