Last active
March 13, 2016 14:46
-
-
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.)
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 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