Created
July 10, 2018 11:21
-
-
Save harishkotra/9293032d7783904553dd9ef65b9c51a1 to your computer and use it in GitHub Desktop.
Java program to reverse a string without worrying about a null.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReverseString{ | |
public static void main(String []args){ | |
String myString = "this is the test"; | |
System.out.println(reverse(myString)); | |
} | |
public static String reverse(String str){ | |
System.out.println("Original Length " + str.length()); | |
char[] data = str.toCharArray(); | |
int i = 0; | |
int j = data.length - 1; | |
char temp; | |
while(i<j){ | |
temp = data[i]; | |
data[i] = data[j]; | |
data[j] = temp; | |
i++; | |
j--; | |
} | |
String s = new String(data); | |
System.out.println("Modified Length " + s.length()); | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment