如何将输入的英文反过来输出?

JamnyXie 2009-09-16 12:13:59
如何将输入的英文反过来输出。
例如输入: nice to meet you!
输出就是: !you meet to nice

public 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) {
//在这里写实现代码
}

}
...全文
592 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
topppppp 2009-11-03
  • 打赏
  • 举报
回复
我做的。例:String str1 = "nice to meet you!";
打印出的结果是: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;
}
}
bigbug9002 2009-09-16
  • 打赏
  • 举报
回复


import java.util.*;
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();
}
浴火涅磐 2009-09-16
  • 打赏
  • 举报
回复
System.out.println(new StringBuilder(str).reverse().toString());
就可以了,在java中游转移的方法直接调用就可以了
bigbro001 2009-09-16
  • 打赏
  • 举报
回复
提供2种思路:
第一种通过字符数组

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.");
}
}
dream_wu 2009-09-16
  • 打赏
  • 举报
回复
public 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) {
//在这里写实现代码
int i = str.length();
while(i > 0){
System.out.print(str.substring(i-1,i));
i--;
}
}

}
yuyeyi 2009-09-16
  • 打赏
  • 举报
回复
分隔符试试
logon29 2009-09-16
  • 打赏
  • 举报
回复
难搞。 i'm fine 吧
JamnyXie 2009-09-16
  • 打赏
  • 举报
回复
dollyn 这个是全部反过来的。
我需要的是 输入: hello world.
输出: .world hello
dream_wu 2009-09-16
  • 打赏
  • 举报
回复
可以把各个字符拆开存到数组里,然后倒序取出来就行了
JamnyXie 2009-09-16
  • 打赏
  • 举报
回复
我是没找到,所以才来问的。 自己想了挺久的,思路是有,但是发现写不出来。。
霜之哀伤 2009-09-16
  • 打赏
  • 举报
回复
StringBuffer自带了方法reverse,所以:

StringBuffer strBuffer = new StringBuffer(str);
strBuffer.reverse();
System.out.println(strBuffer.toString());
  • 打赏
  • 举报
回复
这种题目请先行自己做,做不出来的时候,再来 CSDN 问你所碰到的问题。而不是一上来就让人写所有的代码!
yanliang_xt 2009-09-16
  • 打赏
  • 举报
回复
System.out.println(new StringBuilder(str).reverse().toString());
  • 打赏
  • 举报
回复
自己搜索一下,CSDN 有很多这样的帖子
bigbug9002 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 logon29 的回复:]
引用 15 楼 logon29 的回复:
楼上的, 楼主给的 字符串语法不对。 如果是 i'm fine ,反转后应该是 fine i'm, 你这个匹配好像就不能用了。

是13楼
[/Quote]
如果'号不当成标点.你就把String regex="[^\\w]"改为:
String regex="[^\\w']"
timeriver_wang 2009-09-16
  • 打赏
  • 举报
回复
带有
new StringBuilder(str).reverse().toString()
的都不对
logon29 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 logon29 的回复:]
楼上的, 楼主给的 字符串语法不对。 如果是 i'm fine ,反转后应该是 fine i'm, 你这个匹配好像就不能用了。
[/Quote]
是13楼
timeriver_wang 2009-09-16
  • 打赏
  • 举报
回复
补充一下结果:
! hao jia da Word Hello
logon29 2009-09-16
  • 打赏
  • 举报
回复
楼上的, 楼主给的 字符串语法不对。 如果是 i'm fine ,反转后应该是 fine i'm, 你这个匹配好像就不能用了。
timeriver_wang 2009-09-16
  • 打赏
  • 举报
回复
楼主 ,我的比较好理解:

public class MyString {
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就会在字符串结尾多出一个空格,要去掉的
*
*/

}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧