Skip to content

Instantly share code, notes, and snippets.

@dkohlsdorf
Last active March 13, 2019 21:00
Show Gist options
  • Select an option

  • Save dkohlsdorf/7cdb43f17bf346e3ef34 to your computer and use it in GitHub Desktop.

Select an option

Save dkohlsdorf/7cdb43f17bf346e3ef34 to your computer and use it in GitHub Desktop.
Reverse a String
public class RecerseAString {
public static String reverse(String string) {
char str[] = string.toCharArray();
for(int i = 0; i < str.length / 2; i++) {
char tmp = str[i];
str[i] = str[str.length - 1 - i];
str[str.length - 1 - i] = tmp;
}
return new String(str);
}
public static void main(String[] args) {
System.out.println(reverse("abcgde"));
System.out.println(reverse("abcde"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment