串的简单处理
来源:互联网 发布:1688传淘宝后无法上架 编辑:程序博客网 时间:2024/04/29 22:02
在实际的开发工作中,对字符串的处理是最常见的编程任务。
本题目即是要求程序对用户输入的串进行处理。具体规则如下:
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个字符。
方法一:
import java.util.Scanner;import java.util.Vector;public class Question2 { 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'){ 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) { // TODO: handle exception } }}
方法二:
import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;/* * 本题目即是要求程序对用户输入的串进行处理。具体规则如下: *1. 把每个单词的首字母变为大写。 *2. 把数字与字母之间用下划线字符(_)分开,使得更清晰 *3. 把单词中间有多个空格的调整为1个空格。 *我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。每个单词间由1个或多个空格分隔。 *假设用户输入的串长度不超过200个字符。 */public class SimpleString { public static void main(String args[]){ String str=""; str=(new Scanner(System.in)).nextLine(); String []str1=str.split("[ ]+"); for(int i=0;i<str1.length;i++)str1[i]=String.valueOf((char)(str1[i].charAt(0)+('A'-'a')))+str1[i].substring(1); String s=""; for(int i=0;i<str1.length-1;i++)//System.out.print(str1[i]+" "); { s+=str1[i]+" "; } s+=str1[str1.length-1]; Pattern p=Pattern.compile("([0-9]+)"); Matcher m=p.matcher(s); String fin=""; int st=0; while(m.find()){ int start=m.start(); int end=m.end(); fin+=s.substring(st,start); if(s.charAt(start-1)!=' ')fin+="_"; fin+=m.group(1); if(s.charAt(end)!=' ')fin+="_"; st=end; } if(st<s.length())fin+=s.substring(st); System.out.println(fin); }}
方法三:
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo02 { 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]); while(m.find()){ 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); } }
运行结果:
you and me what cpp2005program
You And Me What Cpp_2005_program
0 0
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 串的简单处理
- 蓝桥杯 串的简单处理
- 【蓝桥杯】【串的简单处理】
- 蓝桥 串的简单处理
- ytu 1304:串的简单处理
- 蓝桥杯 串的简单处理 解题报告
- Java算法--串的简单处理
- OJ 刷题---串的简单处理
- 算法练习-串的简单处理
- Math.random()-实现浏览器验证码点击刷新的JS方法核心
- JSON-基础知识及实例应用
- linux压缩解压之格式浅谈
- Android中的Loaders机制(5)
- 【上下界网络流】sgu194 zoj3229 sgu176 zoj1994 zoj3496
- 串的简单处理
- 关于Qt creator的android配置
- Hadoop1.0架构思考
- 5-18 二分法求多项式单根
- 【多重二分匹配+网络流】:poj2112,Optimal Milking
- Installing pytesser
- 大数据盘点之Spark篇201512
- web前端图片极限优化策略
- 19.shell脚本编程(4)