java字符串翻转
2016-04-20 10:51
381 查看
问题描述:翻转一个字符串,并把小写变成大写 ,大写变成小写。
代码实现:
代码实现:
package leetcoder; import java.util.*; public class Solution1 { public static String trans(String s, int n) { if (s == null) return null; char[] ch = new char[n + 1]; for (int i = 0; i < n; i++) { char c = s.charAt(i); if (c >= 'a' && c <= 'z') ch[i] = (char) (c - 32); else if (c >= 'A' && c <= 'Z') ch[i] = (char) (c + 32); else ch[i] = c; } ch = '\0'; int i = 0, j = s.length() - 1; while (i < j) { char temp = ch[i]; ch[i] = ch[j]; ch[j] = temp; i++; j--; } i = 0; j = 0; while (j != n + 1 && i != n + 1) { if (ch[i] == ' ') { i++; j++; } else if (ch[j] == ' ' || ch[j] == '\0') { int tempj = j; int tempi = i; --tempj; while (tempi < tempj) { char temp = ch[tempi]; ch[tempi] = ch[tempj]; ch[tempj] = temp; tempi++; tempj--; } i = ++j; } else { j++; } } String str = new String(ch); return str; } public static void main(String[] args) { String s = " This is a simple"; System.out.println(trans(s, s.length()).toString()); } }
相关文章推荐
- Java类初始化完整步骤
- Java多线程编程:
- java的final关键字——修饰变量
- 工作问题-java-ajax返回一对多关系数据显示
- 类和对象(java面向对象篇)
- Spring的CharacterEncodingFilter过滤字符编码
- jsp传list到spring mvc
- 导入maven项目遇到中文乱码
- Java多线程代码示例
- Java 基本数据类型转换
- 应用eclipse自带工具进行代码调用函数分析
- STS (Spring Tool Suite) 导入getting started guide提示SunCertPathBuilderException错误的解决
- Java迭代器的一般用法
- java file类
- Spring AOP切面基础 实现请求参数的验证
- Spring data jpa批量插入和更新
- 配置org.springframework.scheduling.quartz.CronTriggerBean
- 【转】Eclipse工具使用技巧总结
- eclipse 启动报错 Ensure that the org.eclipse.core.runtime bundle is resolved
- java和javac的基本用法