Without going through the char sequence is there any way to reverse String
in Java
12 Answers
Try this,
String s = "responses";
StringBuilder builder = new StringBuilder(s);
System.out.println(builder.reverse());
You can use the StringBuilder#reverse()
method:
String reverse = new StringBuilder(originalString).reverse().toString();
Use StringBuilder's
or StringBuffer's
method... reverse()
public class StringReverse
{
public static void main(String[] args)
{
String string=args[0];
String reverse = new StringBuffer(string).reverse().toString();
System.out.println("\nString before reverse: "+string);
System.out.println("String after reverse: "+reverse);
}
}
StringBuffer
is thread-safe, where as StringBuilder
is Not thread safe..... StringBuilder
was introduced from Java 1.5, as to do those operations faster which doesn't have any Concurrency to worry about....
-
-
1You should use StringBuilder vs. StringBuffer, unless you have concurrency problems to solve.– assyliasSep 6, 2012 at 17:56
-
Try reverse() method:
StringBuilder stringName = new StringBuilder();
String reverse = stringName.reverse().toString();
-
Are you sure that this method exists? docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html Sep 7, 2012 at 13:31
-
You may use StringBuilder..
String word = "Hello World!";
StringBuilder sb = new StringBuilder(word);
System.out.print(sb.reverse());
If we have to do it:
Without going through the char sequence
One easy way with iteration will be:
public String reverse(String post) {
String backward = "";
for(int i = post.length() - 1; i >= 0; i--) {
backward += post.substring(i, i + 1);
}
return backward;
}
You can use String buffer to reverse a string.
public String reverse(String s) {
return new StringBuffer(s).reverse().toString();
}
one more interesting way to do this is recursion.
public String reverse(String s) {
if (s.length() <= 1) {
return s;
}
return reverse(s.substring(1, s.length())) + s.charAt(0);
}
This is a way to do so using recursion -
public static String reverse(String s1){
int l = s1.length();
if (l>1)
return(s1.substring(l-1) + reverse(s1.substring(0,l-1)));
else
return(s1.substring(0));
}
Using minimal API support. A simple algorithm.
static String reverse(String str) {
char[] buffer = str.toCharArray();
for (int i = 0; i < buffer.length/2; ++i){
char c = buffer[i];
buffer[i] = buffer[buffer.length-1-i];
buffer[buffer.length-1-i] = c;
}
return new String(buffer);
}
-
Please read the question again. How do you understand
Without going through the char sequence
?– catch23Oct 25, 2018 at 16:45
Here I have a sample of the same using substring method and o(n) without using any nethods from string . I am aware that using substring will hold complete string memory.
for(int i = 0; i < s.length(); i++) {
s = s.substring(1, s.length() - i) + s.charAt(0) + s.substring(s.length() - i);
System.out.println(s);
}
This might help you!!
public class RevString {
public static void main(String[] args) {
String s="jagan";
String rev="";
for (int i=s.length()-1;i>=0;i--) {
rev=rev+s.charAt(i);
}
System.out.println("Reverse String is: "+rev);
}
}
-
1
I have not seen any easy way. Here is the suitable way to do:
Using the loop:
String d = "abcdefghij";
char b[] = new char[d.length()];// new array;
int j=0; // for the array indexing
for(int i=d.length()-1;i>=0;i--){
b[j] = d.charAt(i); // input the last value of d in first of b i.e. b[0] = d[n-1]
j++;
}
System.out.println("The reverse string is: "+String.valueOf(b));
Output is
The reverse string is: jihgfedcba
The simple logic is:
array[i] = array[n-i];
where i is the Iteration and n is the total length of array
-
Please read the question again. How do you understand
Without going through the char sequence
?– catch23Oct 25, 2018 at 16:52 -