Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Name: Method practice - String reverse (array)
- * @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 {
- 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: ");
- } //while end
- input.close();
- } //main end
- public static String reverse( String str ){
- int cnt_up=0;
- int length = str.length();
- char[] charArray = new char[length];
- for(int cnt_down=length-1 ; cnt_down>=0 ; cnt_down--){
- charArray[cnt_up] = str.charAt(cnt_down);
- cnt_up++;
- } //for end
- String str_reverse = new String(charArray);
- return str_reverse;
- } //reverse() end
- } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement