Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
1.2 Write code to reverse a C-Style String. (C-String means that “abcd” is represented as !ve characters, including the null character.)
public class StringReverse {
public static void main(String args[]){
String s = "this is a demo test!";
System.out.println(reverse(s));
}
public static String reverse(String input){
char[] data = input.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);
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment