When.com Web Search

Search results

  1. Results From The WOW.Com Content Network
  2. How to reverse words of a Java String - Stack Overflow

    stackoverflow.com/questions/15910092

    I did suggest first reverse the whole string. Then reverse the substring between two spaces. public class ReverseByWord { public static String reversePart (String in){ // Reverses the complete string String reversed = ""; for (int i=0; i<in.length(); i++){ reversed=in.charAt(i)+reversed; } return reversed; } public static String reverseByWord (String in){ // First reverses the complete string ...

  3. Reverse a string in Java - Stack Overflow

    stackoverflow.com/questions/7569335

    2. One natural way to reverse a String is to use a StringTokenizer and a stack. Stack is a class that implements an easy-to-use last-in, first-out (LIFO) stack of objects. String s = "Hello My name is Sufiyan"; Put it in the stack frontwards. Stack<String> myStack = new Stack<>();

  4. You split the string by the space then iterate over it backwards to assemble the reversed sentence. String[] words = "This is interview question".split(" "); String ...

  5. 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 complicated, it also has ...

  6. What is the most efficient way to reverse a string in Java? Should I use some sort of xor operator? The easy way would be to put all the chars in a stack and put them back into a string again but I

  7. Reverse String Word by Word in Java - Stack Overflow

    stackoverflow.com/questions/9105277

    1. First of all you should decouple it in three functions. The first breaking the big string in a list of strings using the space as delimiter, the second reversing one string without spaces, and the last concatenating strings. When you do that it will be easier to locate what cause the space to appears.

  8. 108. This should do the trick. This will iterate through each word in the source string, reverse it using StringBuilder 's built-in reverse () method, and output the reversed word. String source = "Hello World"; for (String part : source.split (" ")) { System.out.print (new StringBuilder (part).reverse ().toString ()); System.out.print (" "); }

  9. Your problem is that you're asking it to print the whole string repeatedly in this line: System.out.print(arr[j]+" ");.

  10. 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 ...

  11. I was asked this in one of the interview. how to reverse the words in string in java? Example: String [] s ="Cat sat on sofa" will turn to s ="sofa on sat cat" modifying the same s. I said : public String reverse (String [] s) {. String reverse = ""; for(int i = (s.length -1); i>=0; i--) reverse = reverse + s[i] + " ";