如何将输入的英文反过来输出。
例如输入: nice to meet you!
输出就是: !you meet to nicepublic class ReverseOutput {
public static void main(String[] args) {
String s = "hello world.how are you?i fine.and you?nice to meet you!me to!";
new Reverse().doRoput(s);
}
}class Reverse {
Reverse() {} public void doRoput(String str) {
//在这里写实现代码
}}
例如输入: nice to meet you!
输出就是: !you meet to nicepublic class ReverseOutput {
public static void main(String[] args) {
String s = "hello world.how are you?i fine.and you?nice to meet you!me to!";
new Reverse().doRoput(s);
}
}class Reverse {
Reverse() {} public void doRoput(String str) {
//在这里写实现代码
}}
strBuffer.reverse();
System.out.println(strBuffer.toString());
我需要的是 输入: hello world.
输出: .world hello
public static void main(String[] args) {
String s = "hello world.how are you?i fine.and you?nice to meet you!me to!";
new Reverse().doRoput(s);
}
}class Reverse {
Reverse() {} public void doRoput(String str) {
//在这里写实现代码
int i = str.length();
while(i > 0){
System.out.print(str.substring(i-1,i));
i--;
}
}}
第一种通过字符数组import java.util.*;public class Palindrome1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String word = sc.next();
int i = word.length();
int j = 0;
while (j <= (i / 2) -1 && word.charAt(j) == word.charAt(i - j - 1))
j++;
if (j == i / 2)
System.out.println("The string is palindrome.");
else
System.out.println("The string is not palindrome.");
}
}第二种通过StringBuffer的方法import java.util.*;public class Palindrome2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String word = sc.next();
if (word.equals(new StringBuffer(word).reverse().toString()))
System.out.println("The string is palindrome.");
else
System.out.println("The string is not palindrome.");
}
}
就可以了,在java中游转移的方法直接调用就可以了
import java.util.regex.*;
//....
public void doRoput(String str) {
Stack<String> reverseString=new Stack<String>();
String regex="[^\\w]";
Matcher m=Pattern.compile(regex).matcher(str);
int start=0,end=0;
while(m.find()){
if(start==m.start()){
reverseString.push(m.group());
}else{
reverseString.push(str.substring(start,m.start()));
reverseString.push(m.group());
}
start=m.end();
}
if(start!=str.length()-1){
reverseString.push(str.substring(start));
}
while(!reverseString.empty()){
System.out.print(reverseString.pop());
}
System.out.println();
}
public static void main(String[] args) {
String string="Hello Word da jia hao !";
String[] strs=string.split(" ");
StringBuilder sb=new StringBuilder();
for(int i=strs.length-1;i>0;i--){
sb.append(strs[i]+" ");
}
sb.append(strs[0]);
String newStr=sb.toString();
System.out.println(newStr);
}
/**
* 如果把
* for(int i=strs.length-1;i>0;i--){
* sb.append(strs[i]+" ");
* }
* sb.append(strs[0]);
* 换成
* for(int i=strs.length-1;i>=0;i--){
* sb.append(strs[i]+" ");
* }
* sb就会在字符串结尾多出一个空格,要去掉的
*
*/}
! hao jia da Word Hello
new StringBuilder(str).reverse().toString()
的都不对
String regex="[^\\w']"
打印出的结果是:ecin ot teem !uoy (控制每个单词reverse)public class Test {
public static void main(String[] args) {
String str1 = "nice to meet you!";
String [] a = str1.split(" ");
changeArray(a);
}
public static void changeArray(String []a){
for(int i = 0; i < a.length; i++){
System.out.print(changeString(a[i])+" ");
}
}
public static String changeString(String str){
char []b = str.toCharArray();
String str1 = "";
for(int i =b.length - 1 ; i >= 0 ; i-- ){
str1 = str1 + b[i];
}
return str1;
}
}