tag:blogger.com,1999:blog-694855878384792308.post1388448527533950841..comments2024-03-21T06:26:49.387-07:00Comments on Java67: [Solved] How to reverse a String in place in Java? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-694855878384792308.post-81859077610917368812022-03-30T20:43:51.853-07:002022-03-30T20:43:51.853-07:00here's the simplest way using a for loop, I th...here's the simplest way using a for loop, I think<br />public static String reverseString(String s){<br /> char [] a = s.toCharArray();<br /> for(int i=0, j=s.length()-1; i<j; i++, j--) {<br /> char temp = a[i];<br /> a[i] = a[j];<br /> a[j] = temp;<br /> }<br /> return new String(a);<br /> }Anonymoushttps://www.blogger.com/profile/09102916737569398864noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-4012352621140880382022-02-07T03:14:32.034-08:002022-02-07T03:14:32.034-08:00//This is Using the toCharArray() function to reve...//This is Using the toCharArray() function to reverse a string in Java<br /><br />public static String reverse(String str) <br />{<br /> String rev="";<br /> char[] finalarray = str.toCharArray();<br /> for (int i = finalarray.length - 1; i >= 0; i--)<br /> rev+=finalarray[i];<br /> return rev;<br />}<br />//Sample Input- "Scaler Academy"<br />//Sample Output-"ymedacA relacS"<br /><br /><br /><br /><br />Reference: https://www.scaler.com/topics/reverse-a-string-in-java/Sukesh Rhttps://www.scaler.com/topics/reverse-a-string-in-java/noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-55555518092371902832021-09-24T09:42:49.241-07:002021-09-24T09:42:49.241-07:00//THIS CODE WILL GIVE THE DESIRED RESULT. ... //THIS CODE WILL GIVE THE DESIRED RESULT. <br /><br /> string str;<br /> cin>>str;<br /> for(int i=str.size(); i>=0; i--)<br /> {<br /> cout<<str[i];<br /> } Anonymoushttps://www.blogger.com/profile/04436122931973258497noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-41036583081602601872020-06-16T07:14:27.384-07:002020-06-16T07:14:27.384-07:00public class MyClass {
public static void main...public class MyClass {<br /> public static void main(String args[]) {<br /> String s= "Nikunj";<br /> Character temp=null;<br /> char[] c= s.toCharArray();<br /> for(int i=0;i<c.length/2;i++){<br /> temp=c[i];<br /> c[i]=c[c.length-(i+1)];<br /> c[c.length-(i+1)]=temp;<br /> }<br /> for(Character ccc:c)<br /> System.out.print(ccc);<br /> }<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-43411548300709245562019-12-17T17:36:11.031-08:002019-12-17T17:36:11.031-08:00//simple & efficient way without even declarin...//simple & efficient way without even declaring 3rd variable<br />public static void reversingUsingSwappingLogic(String str) {<br /> <br /> char[] ca = str.toCharArray();<br /> <br /> for(int i = 0; i < str.length()/2; i++) {<br /> <br /> ca[i] = (char) (ca[i] ^ ca[ca.length - 1 - i]);<br /> ca[ca.length - 1 - i] = (char) (ca[i] ^ ca[ca.length - 1 - i]);<br /> ca[i] = (char) (ca[i] ^ ca[ca.length - 1 - i]);<br /> <br /> }<br /> str = new String(ca);<br /> System.out.println(str);<br /> <br /> }Adityahttps://www.linkedin.com/in/aditya-kadambala-6bb208191/noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-75723002558599113652019-08-09T15:13:53.350-07:002019-08-09T15:13:53.350-07:00void reverse( String A)
{
char[] yArray = new c...void reverse( String A)<br /> {<br /> char[] yArray = new char[A.length()];<br /> char xArray[] =new char[A.length()];<br /> A.getChars(0, A.length(), xArray, 0);<br /> System.out.println(xArray);<br /> int j=xArray.length;<br /> for(int i=0;i<xArray.length; i++)<br /> {<br /> yArray[i]=xArray[j-1];<br /> j--;<br /> System.out.println("character pushed at "+i+" is "+yArray[i]);<br /> }<br /> System.out.println("Reversed String is : ");<br /> System.out.println(yArray);<br /> }<br />Anonymoushttps://www.blogger.com/profile/12338509647856682005noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-67998480197272498132019-06-03T11:26:49.889-07:002019-06-03T11:26:49.889-07:00My mistake! The original article states that the a...My mistake! The original article states that the algorithm is O(1) because it (wrongly) says it only uses 2 new variables regards of size of the string. Because two new arrays of the same size of the string are created the space needed will be either 3N or 2N, depending on whether or not you include the original string. Either way, the space complexity is O(n). That is, the space needed grows linearly with the size of the string.philhttps://www.blogger.com/profile/06027616051394904156noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-63020077553898273632019-06-01T15:13:36.936-07:002019-06-01T15:13:36.936-07:00How come it is O(1) space if this code creates a n...How come it is O(1) space if this code creates a new array of characters?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-4563001454926205952019-06-01T06:33:15.819-07:002019-06-01T06:33:15.819-07:00Sorry, but Strings are immutable in Java. This po...Sorry, but Strings are immutable in Java. This post makes no mention of this extremely important fact. While it is true that the time complexity of the algorithm is O(n) and the space complexity is O(1), both the toCharArray() method and the new String(char[] buffer) constructor make COPIES of their arrays. This example is NOT reversal in place as that's not possible in Java (without unsafe shenanigans) and it does require the use of extra memory (the original String plus at least two array copies.philhttps://www.blogger.com/profile/06027616051394904156noreply@blogger.com