A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 姜秋艺 中级黑马   /  2015-5-22 23:39  /  280 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 姜秋艺 于 2015-5-23 22:08 编辑

键盘输入字符串, abc
输出结果 cba
字符串的反转 != 倒着遍历
实现步骤:
键盘输入
将字符串--转成字符数组  toCharArray
数组的反转 -- 最远端换位
数组变回字符串  -- 构造方法
         

2 个回复

倒序浏览
这个挺简单的  首先char[] ch=s.toCharArry();  得到数组 然后遍历  换位 就OK了  for(int x=0,y=ch.length-1;x<y;x++,y--)然后换位置
回复 使用道具 举报
{public static void main(String[] args)
{

BufferedReader bufr=null;
BufferedWriter bufw=null;
try
        {
       
         bufr=new BufferedReader(new InputStreamReader(System.in));
         
         bufw=new BufferedWriter(new OutputStreamWriter(System.out));
        String line=null;
       
        while((line=bufr.readLine())!=null)
        {       
                char[] arr=line.toCharArray();
               
         reverse(arr);
         bufw.write(new String(arr));
         bufw.newLine();
                bufw.flush();
               
        }
        }
        catch(Exception e)
        {
                throw new RuntimeException("读取失败");
               
        }
finally
{try{if(bufr!=null)
       
                bufr.close();
        }
        catch(Exception e)
        {
                throw new RuntimeException("读取关闭失败");
               
        }
       
        }
try{
        if(bufw!=null){
                bufw.close();
        }
       
       
        }
catch(Exception e)
        {
        throw new RuntimeException("输出关闭失败");
       
        }

       
        }


public static void reverse(char[] arr)
{for(int start=0,end=arr.length-1;start<end;start++,end--)
       
        swap(arr,start,end);


}

public static void swap(char[] arr,int x,int y)
{char temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马