Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Name: Method practice - String reverse (recursive method)
- * @author LinChuWen
- * Date: 2014.10.22
- *
- * NCHU EE,course number:2335
- * course name: Object Oriented Language
- * Textbook: Big Java:Late Objects-Cay S. Horstmann
- * Problem: P5.15
- * Description: Input a string, reverse it, print out the result.
- */
- import java.util.*;
- public class HW4_P5_15_v2 {
- private static int counter=0;
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Please input a string: ");
- while(input.hasNext()){
- String str_input = input.nextLine();
- System.out.printf("reverse result: %s\n\n", reverse(str_input));
- System.out.print("Please input a string: ");
- counter=0;
- } //while end
- input.close();
- } //main end
- public static String reverse( String str ){
- counter++;
- if(counter<str.length()-counter+1){
- str = str + str.charAt(str.length()-2*counter);
- return reverse(str);
- } //if end
- else
- return str.substring(counter-1, str.length());
- } //reverse() end
- } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement