如何让一个字符串反转
答案:1 悬赏:0
解决时间 2021-02-11 04:45
- 提问者网友:优雅ぉ小姐
- 2021-02-10 11:50
如何让一个字符串反转
最佳答案
- 二级知识专家网友:放肆的依賴
- 2021-02-10 12:15
package com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class StringReverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = " ";
StringBuffer retBuffer = new StringBuffer("");
try {
string = br.readLine();
} catch (IOException ex) {
System.out.println(ex);
}
char[] retChar = new char[string.length()];
for (int i = string.length() - 1, j = 0; i >= 0; i--, j++) {
retChar[j] = string.charAt(i);
}
System.out.println(String.valueOf(retChar));
StringBuffer sb = new StringBuffer(string);
System.out.println(sb.reverse().toString());
System.out.println(reverse(string));
}
public static String reverse(String in) {
String input = in;
String output;
//执行反转
Stack theStack = new Stack();
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStack.push(ch);
}
output = "";
while (!theStack.isEmpty()) {
char ch = theStack.pop();
output = output + ch;
}
return output;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class StringReverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = " ";
StringBuffer retBuffer = new StringBuffer("");
try {
string = br.readLine();
} catch (IOException ex) {
System.out.println(ex);
}
char[] retChar = new char[string.length()];
for (int i = string.length() - 1, j = 0; i >= 0; i--, j++) {
retChar[j] = string.charAt(i);
}
System.out.println(String.valueOf(retChar));
StringBuffer sb = new StringBuffer(string);
System.out.println(sb.reverse().toString());
System.out.println(reverse(string));
}
public static String reverse(String in) {
String input = in;
String output;
//执行反转
Stack
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStack.push(ch);
}
output = "";
while (!theStack.isEmpty()) {
char ch = theStack.pop();
output = output + ch;
}
return output;
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯