Java算法--串的简单处理
2023-09-11 14:21:00 时间
题目例如以下:
串的处理
在实际的开发工作中。对字符串的处理是最常见的编程任务。
本题目即是要求程序对用户输入的串进行处理。详细规则例如以下:
1. 把每个单词的首字母变为大写。
2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
3. 把单词中间有多个空格的调整为1个空格。
比如:
用户输入:
you and me what cpp2005program
则程序输出:
You And Me What Cpp_2005_program
用户输入:
this is a 99cat
则程序输出:
This Is A 99_cat
我们如果:用户输入的串中仅仅有小写字母,空格和数字。不含其他的字母或符号。
每个单词间由1个或多个空格分隔。
如果用户输入的串长度不超过200个字符。
方法一:
public class 串的简单处理 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
Vector<Character> vector = new Vector<Character>();
for (int i = 0; i < string.length(); i++) {
vector.add(string.charAt(i));
}
try {
int index = 0;
while (index < vector.size()) {
//推断第一个是否为小写的英文字符,是的话进行操作
if (index == 0 && vector.elementAt(index) >= 'a'
&& vector.elementAt(index) <= 'z') {
//Replaces the element at the specified position in this Vector with the specified element
vector.set(index,(char) (vector.elementAt(index) - ('a' - 'A')));
} else if (vector.elementAt(index - 1) == ' '&& vector.elementAt(index) == ' ') {
//处理有多个空格的可能
vector.remove(index);
index--;
} else if (vector.elementAt(index - 1) == ' '
&& (vector.elementAt(index) >= 'a' && vector
.elementAt(index) <= 'z')) {
//推断是空格后边的字符
vector.set(index,
(char) (vector.elementAt(index) - ('a' - 'A')));
} else if ((vector.elementAt(index) >= 'a' && vector
.elementAt(index) <= 'z')
&& (vector.elementAt(index - 1) >= '0' && vector
.elementAt(index - 1) <= '9')) {
vector.add(index, '_');
index++;
} else if ((vector.elementAt(index - 1) >= 'a' && vector
.elementAt(index - 1) <= 'z')
&& (vector.elementAt(index) >= '0' && vector
.elementAt(index) <= '9')) {
//推断的是数字
vector.add(index, '_');
index++;
}
index++;
}
for (int i = 0; i < vector.size(); i++) {
System.out.print(vector.elementAt(i));
}
System.out.println();
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
方法二:主要用到正則表達式对字符串进行截取,然后对每个字符数组的元素进行正则匹配,含有数字的单独进行处理
public class SimpleString {
// 打印字符串的函数
public static void print(String[] s) {
for (int i = 0; i < s.length - 1; i++) {
System.out.print(s[i] + " ");
}
System.out.println(s[s.length - 1]);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
String[] ss = s.split("[\\s]+"); // 依据正則表達式,删除一个或多个空格,将字符串保存为字符数组
for (int i = 0; i < ss.length; i++) {
// 将每个字符数组的首字母改为大写
String up = ("" + ss[i].charAt(0)).toUpperCase(); // 大写
StringBuffer sb = new StringBuffer(ss[i]);
ss[i] = sb.replace(0, 1, up).toString();
// 上边已经把字符串数组的首字母该为大写。然后对更改后的字符数组推断是否有数字
Matcher m = Pattern.compile("\\d+").matcher(ss[i]);// 0-9出现一次或多次
while (m.find()) {
// m.group():Returns the input subsequence matched by the previous match
String num = new String(m.group());
String num2 = num;
num2 = "_" + num + "_"; // 数字前后都加入"_"
ss[i] = ss[i].replace(num, num2);
if (ss[i].startsWith("_")) { // 去头"_"
ss[i] = ss[i].substring(1);
}
if (ss[i].endsWith("_")) { // 去尾"_"
ss[i] = ss[i].substring(0, ss[i].length() - 1);
}
}
}
print(ss);
}
}
相关文章
- Java 数组
- Java 获取当前时间
- 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】
- LeetCode 6-10 题 详解 Java版 ( 万字 图文详解 LeetCode 算法题6-10 =====>>> <建议收藏>)
- LeetCode 16-20 题 详解 Java版 ( 万字 图文详解 LeetCode 算法题16-20 =====>>> <建议收藏>)
- Java中数组有什么特征?
- Java: Thread Pool
- JAVA础--利用java反射机制绕过编译时的类型检查和访问控制检查
- Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结
- 第42节:Java知识点回顾复习
- 第七节:详细讲解Java中的日期,java.util.date
- 基于Java(SSM框架)+MySQL 开发的停车场管理系统【100010071】
- Java中各种排序算法
- Java字节转换类实现
- Java+Nginx获取用户的真实IP
- Error running 'dt-assets-monitor [clean]': Cannot run program "C:Program Files (x86)Javajdk1.8.0_73binjava.exe" (in directory "E:codedt-assets-monitor")
- Java里日期转换及日期比较大小
- RSA加解密算法的Java实现
- 【Java异常】idea 报错:无效的目标发行版:17 的解决办法
- java如何遍历hashMap
- Java基础——异常机制
- 华为OD机试 -日志采集系统(Java) | 机试题+算法思路+考点+代码解析 【2023】
- 华为OD机试 -查找重复代码(Java) | 机试题+算法思路+考点+代码解析 【2023】
- 华为OD机试 -内存资源分配(Java) | 机试题+算法思路+考点+代码解析 【2023】
- 华为OD机试 -入栈出栈(Java) | 机试题+算法思路+考点+代码解析 【2023】
- Java高手速成 | 对象-关系的映射、映射对象标识符与JPA API的级联操作
- Java 判断字符串是否相同(递归)
- JAVA内存结构详解
- How to improve Java's I/O performance( 提升 java i/o 性能)
- 简单的JAVA MVC框架模式--Java-servlet-JavaBean
- java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
- Java算法——大O记法