java-字符串反转

package com.day7.one;

import java.util.Scanner;

public class Test2 {

  /**
  * 字符串反转:"abcd"到"dcba"
  */
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String s1=sc.nextLine();//将键盘的录入的字符串存储在s1中
    char[] s2=s1.toCharArray();
    for (int i = 0; i < (s2.length)/2; i++)
    {
      char c;
      c=s2[i];
      s2[i]=s2[s2.length-1-i];
      s2[s2.length-1-i]=c;
    }
    //方法1
    for (int i = 0; i < s2.length; i++)
    {
      System.out.print(s2[i]);
    }
    //方法2,推荐使用方法2
    String s3=String.valueOf(s2);//将字符数组转成字符串
    System.out.println(s3);
  }

}

posted @ 2017-12-26 21:51  简简单单zjl  阅读(236)  评论(0编辑  收藏  举报