Skip to content

Instantly share code, notes, and snippets.

@harishkotra
Created July 10, 2018 11:21
Show Gist options
  • Save harishkotra/9293032d7783904553dd9ef65b9c51a1 to your computer and use it in GitHub Desktop.
Save harishkotra/9293032d7783904553dd9ef65b9c51a1 to your computer and use it in GitHub Desktop.
Java program to reverse a string without worrying about a null.
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