eBay Interview Question: Write a code in java to read ... | Glassdoor

Interview Question

QA Engineer Interview

Write a code in java to read a file and then reverse its

  contents. Like if its written " My interview was bad" It should reverse in a way "bad was interview my" and not "dab saw weivretni ym"
Answer

Interview Answer

1 Answer

0

public static String reverseAsIs(String original){
            String[] strArray = original.split(" ");
            String[] reverse = new String[strArray.length];
            int len = strArray.length-1;
            for(int i=len; i>=0;i--){
                reverse[len-i]=strArray[i];
            }
            StringBuilder sb = new StringBuilder();
            for (String r : reverse)
            {
                sb.append(r);
                sb.append(" ");
            }

            return sb.toString();
    }

HK on May 21, 2017

Add Answers or Comments

To comment on this, Sign In or Sign Up.