您的位置:首页 > 其它

String 反向输出字符串

2013-10-16 23:40 211 查看
package String;

/**
* 反向输出字符串
*/

import java.util.Scanner;

public class reversal {

public static void main(String[] args) {
System.out.println("Please enter the String:");
String str = new Scanner(System.in).nextLine(); // 输入字符串
toReverse1(str);
toReverse2(str);
}

public static void toReverse1(String str) {
StringBuffer s = new StringBuffer(str);
System.out.println(s.reverse().toString());
}

<pre name="code" class="java">	/**
* 字符串倒转
* 不运用StringBuffer
*/
public static void toReverse2() {
String s = "12345";
char[] chars = s.toCharArray();
int j = chars.length - 1;
for(int i = 0; i < j/2; i++) {
char c = chars[i];
chars[i] = chars[j - i];
chars[j - i] = c;
}
s = new String(chars);
System.out.println(s);
}


}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐