Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ReverseString {
- public static void main(String[] args) {
- String s = "Zack";
- System.out.println("The reverse of " + s + " is " + reverse(s));
- s = null;
- System.out.println("The reverse of " + s + " is " + reverse(s));
- }
- private static String reverse(String s) {
- String reversed = "";
- if (s == null){
- System.out.println("S is null");
- return null;
- }else{
- int i;
- for ( i= s.length()-1; i >= 0; i--){
- if (i != -1){
- char NextLetter= s.charAt(i);
- System.out.println (NextLetter);
- }
- else{
- System.exit (0);
- }
- reversed= reversed + s.charAt(i);
- }
- return reversed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement