025 反转句子

1.题目

  假设有一个句子you are good ,反转句子为good are you。

  不能使用split等函数。

 

2.思路

  这里可以使用Stack。

 

3.程序

 1 import java.util.Stack;
 2 
 3 public class word {
 4     //main函数,方面测试
 5     public static void main(String[] args){
 6         String str="what is your name";
 7         String result=reverseWord(str);
 8         System.out.println(result);
 9     }
10     //这是正式逻辑处理
11     public static String reverseWord(String str){
12         Stack<String> stack=new Stack();
13         String temp="";
14         for(int i=0;i<str.toCharArray().length;i++){
15             if(!(str.charAt(i)==' ')){
16                 temp+=str.toCharArray()[i];
17         }else{
18             stack.push(temp);
19             temp="";
20         }
21     }
22         stack.push(temp);
23     //弹出栈中单词
24     String result="";
25     int size=stack.size();
26         for(int i=0;i<4;i++){
27             result=result+stack.pop()+" ";
28         }
29         return result;
30 }
31 
32 }

 

转载于:https://www.cnblogs.com/juncaoit/p/10504159.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值