问题描述:对于输入的指定字符串,例如“123456789”,实现相应的反转变为“987654321”。

问题分析:先将传入的字符串存储到字符数组里面

代码实现:

 

package com.yonyou.test;

import java.net.MalformedURLException;
import java.net.URISyntaxException;




/**
 * 测试类
 * @author 小浩
 * @创建日期 2015-4-18
 */
public class Test{
	public static void main(String[] args) throws MalformedURLException, URISyntaxException{
    new Test().reverse("123456789");
}

/**
 * 987654321的字符串变成123456789 
 */
public void reverse(String str){
	char[] strChar=str.toCharArray();
	char temp;
	for(int i=0;i<str.length()/2;i++)
	{
		temp=strChar[i];
		strChar[i]=strChar[str.length()-i-1];
		strChar[str.length()-i-1]=temp;
	}
	String newStr=new String(strChar);
	System.out.println("原始的字符串为:"+str);
	System.out.println("当前的字符串为:"+newStr);
}
}

 

  

 

相关文章:

  • 2021-05-29
  • 2021-12-27
  • 2021-06-15
  • 2021-11-28
  • 2022-02-08
  • 2022-02-09
猜你喜欢
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2021-08-02
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案