Last active
August 29, 2015 14:02
-
-
Save WOLOHAHA/5fd7b3cb7308ec906646 to your computer and use it in GitHub Desktop.
Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
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 Main { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Main so = new Main(); | |
so.reverse("abcdefghijk"); | |
} | |
public void reverse(String s){ | |
if(s==null) | |
return; | |
if(s.length()==1) | |
return; | |
char[] c=s.toCharArray(); | |
int i=0; | |
int j=s.length()-1; | |
while(i<j){ | |
char temp=c[i]; | |
c[i]=c[j]; | |
c[j]=temp; | |
i++; | |
j--; | |
} | |
String m=new String(c); | |
s=m; | |
System.out.println(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment