Two ways to invert a string
package com.itheima_07; import java.util.Scanner; /*
* 字符串反转
* 举例:键盘录入”abc”
* 输出结果:”cba”
*
* 分析:
* A:键盘录入一个字符串
* B:写方法实现字符串的反转
* a:把字符串倒着遍历,得到的每一个字符拼接成字符串。
* b:把字符串转换为字符数组,然后对字符数组进行反转,最后在把字符数组转换为字符串
* C:调用方法
* D:输出结果
*/
public class StringTest2 {
public static void main(String[] args) {
//键盘录入一个字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine(); //写方法实现字符串的反转 //调用方法
String result = reverse(s); //输出结果
System.out.println("result:"+result);
} /*
* 把字符串倒着遍历,得到的每一个字符拼接成字符串。
*
* 两个明确:
* 返回值类型:String
* 参数列表:String s
*/ /*
public static String reverse(String s) {
String ss = ""; for(int x=s.length()-1; x>=0; x--) {
ss += s.charAt(x);
} return ss;
}
*/ //把字符串转换为字符数组,然后对字符数组进行反转,最后在把字符数组转换为字符串
public static String reverse(String s) {
//把字符串转换为字符数组
char[] chs = s.toCharArray(); //对字符数组进行反转
for(int start=0,end=chs.length-1; start<=end; start++,end--) {
char temp = chs[start];
chs[start] = chs[end];
chs[end] = temp;
} //最后在把字符数组转换为字符串
String ss = new String(chs);
return ss;
}
}
Two ways to invert a string的更多相关文章
- HDU5863 cjj's string game(DP + 矩阵快速幂)
题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...
- [LintCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- uav 11258 String Partition (DP)
Problem F - String Partition ...
- AtCoder Grand Contest #026 C - String Coloring
Time Limit: 3 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement You are given a st ...
- Android Lint Checks
Android Lint Checks Here are the current list of checks that lint performs as of Android Studio 2.3 ...
- 【NFC】Android NFC API Reference中英文
0 Near Field Communication Near Field Communication (NFC) is a set of short-range wireless technol ...
- JavaScript Patterns 2.8 Number Conversions with parseInt()
Strings that start with 0 are treated as octal numbers (base 8) in ECMAScript 3; however, this has c ...
- 给Asp.net MVC Forms 验证设置角色访问控制
当我们使用Asp.net MVC Forms方式验证用户, 然后设置Controller 或 Action 的 Authorize属性时, 默认情况下只有Users属性可以设置(这里的Users通常是 ...
- 编写高质量js代码
原文链接:http://code.tutsplus.com/tutorials/24-javascript-best-practices-for-beginners--net-5399 jquery代 ...
随机推荐
- Maven私服架设(nexus / on windows)
Maven私服可以用多个不同的产品可供选择,下面我们演示使用最为广泛的nexus来架设maven本地私服 Nexus的下载及安装请见官方下载页: http://www.sonatype.org/n ...
- Redis客户端使用
http://wenku.baidu.com/view/6ccd650af12d2af90242e63d.html 一.下载jedis 代码 jedis 代码地址:https://github.com ...
- (转)关于IBM小机P520的面板使用
原文:http://www.xlgps.com/article/390810.html 经过搜索资料及自己的试验,现将解决方法记录如下.P520控制面板上有三个按钮,按钮上方有一个显示屏,上面显示你操 ...
- windows 安装openssl
参考文章:http://www.cnblogs.com/tangxin-blog/p/5724071.html 准备条件: 1.VS2012 (携带c++) 2.下载openssl 源码 3.安装Pe ...
- Android 开发工具类 07_ScreenUtils
获得屏幕相关的辅助类: 1.获得屏幕高度: 2.获得屏幕宽度: 3.获得状态栏的高度: 4.获取当前屏幕截图,包含状态栏: 5.获取当前屏幕截图,不包含状态栏. import android.app. ...
- Phoenix 4.8
From v4.8.0 onwards, user can enable to map it’s schema to the namespace so that any table created w ...
- Spring Boot 的彩色日志
springboot的彩色日志灰常漂亮, 看起来也很舒服. 但是自定义的日志就是一纯白色的, 丑到不行. 所以就copy他的彩色日志来养眼: <!-- 彩色日志 --> <!-- 彩 ...
- AndroidStudio报错Software caused connection abort: recv failed
Software caused connection abort: recv failed 这个问题网上有一种说法 已知会导致这种异常的一个场景如下: 客户端和服务端建立tcp的短连接,每次客户端发送 ...
- java基本类型自动转换
具体自动提升类型如上图所示.其中long->float的转换一开始让我感觉有点问题,因为long是64位的,而float却是32位的. 遂找寻答案,参考博客java中long到float的自动转 ...
- POJ 1251 Jungle Roads(Kruskal算法求解MST)
题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...