Skip to content

Instantly share code, notes, and snippets.

@rr-paras-patel
Last active March 13, 2016 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rr-paras-patel/b33fe8d5aee0f326cd2e to your computer and use it in GitHub Desktop.
Save rr-paras-patel/b33fe8d5aee0f326cd2e to your computer and use it in GitHub Desktop.
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