Bloomberg L.P. Interview Question: reverse the words in a string... | Glassdoor

Interview Question

Software Engineer Interview(Student Candidate)

reverse the words in a string

Answer

Interview Answer

1 Answer

0

public static String reverse(String input) {
        StringBuilder main = new StringBuilder("");
        StringBuilder sb = new StringBuilder("");
        for (int i = input.length() - 1; i >= 0; i--) {
            System.out.println("SB " + sb);
            if (input.charAt(i) == ' ') {
                main = sb.append(" ").append(main);
                sb = new StringBuilder("");
            } else {
                sb.append(input.charAt(i));
            }

        }
        main = sb.append(" ").append(main);
        return main.toString();

    }

Anonymous on Mar 14, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.