Yahoo India Web Search

Search results

  1. Sep 10, 2017 · Since the below method (using XOR) to reverse a string is not listed, I am attaching this method to reverse a string. The Algorithm is based on : 1.(A XOR B) XOR B = A . 2.(A XOR B) XOR A = B. Code snippet:

  2. the idiomatic solution is. String reversed = new StringBuilder(str).reverse().toString(); If, perhaps for educational purposes, you want to solve this by streaming over the string’s characters, you can do it like. String reversed = str.chars() .mapToObj(c -> (char)c) .reduce("", (s,c) -> c+s, (s1,s2) -> s2+s1); This is not only much more ...

  3. Mar 14, 2010 · Whats the best way to recursively reverse a string in Java? 22. Reverse a string in Java, in O(1)? 594.

  4. The function takes the first character of a String - str.charAt(0) - puts it at the end and then calls itself - reverse() - on the remainder - str.substring(1), adding these two things together to get its result - reverse(str.substring(1)) + str.charAt(0) When the passed in String is one character or less and so there will be no remainder left ...

  5. Aug 11, 2017 · Can anyone tell me how to write a Java program to reverse a given sentence? For example, if the input is: "This is an interview question" The output must be: "question interview an is this"

  6. Reversing a Stream prior to Java version 21 without using a customized collection type is more verbose, typically requiring an extra line to reverse the resulting list. The following Java 20 implementation streams to a mutable ArrayList , reverses the list, and then streams the list using .stream() .

  7. May 14, 2009 · Correct me if I'm wrong, but I believe Mehrdad just means in trivial cases like these. Although a recursive solution may actually be simpler to think of as opposed to an iterative solution, I believe it takes up much more of "the stack" to carry out the recursion.

  8. Aug 2, 2016 · 3. Use StringBuilder class or StringBuffer class they have already a method reverse() for reversing the string. StringBuilder str = new StringBuilder("india"); System.out.println("string = " + str); // reverse characters of the StringBuilder and prints it. System.out.println("reverse = " + str.reverse()); // reverse is equivalent to the actual ...

  9. Nov 7, 2014 · First, you could use Arrays.toString(Object[]) to print your String[].Then you can iterate half of the array and swap the String at the current position with it's corresponding pair (at the length - current index, remembering that Java arrays are zero indexed so the last position in an array is array.length - 1).

  10. System.out.println("Enter a sentence to be reversed: "); original = in.nextLine(); // Save the user's input as "original". int length = original.length(); // Get the length of the input. for (int i = length - 1; i >= 0; i--) // Iterate over each character of the input, starting from the end until you reach the beginning and add the character to ...

  1. People also search for