Java算法之串的簡單處理
題目如下:
串的處理
在實際的開發(fā)工作中,對字符串的處理是最常見的編程任務。
本題目即是要求程序對用戶輸入的串進行處理。具體規(guī)則如下:
1. 把每個單詞的首字母變?yōu)榇髮憽?/p>
2. 把數(shù)字與字母之間用下劃線字符(_)分開,使得更清晰
3. 把單詞中間有多個空格的調整為1個空格。
例如:
用戶輸入:
you and me what cpp2005program
則程序輸出:
You And Me What Cpp_2005_program
用戶輸入:
this is a 99cat
則程序輸出:
This Is A 99_cat
我們假設:用戶輸入的串中只有小寫字母,空格和數(shù)字,不含其它的字母或符號。
每個單詞間由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')) { //判斷的是數(shù)字 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) { } } }
方法二:主要用到正則表達式對字符串進行截取,然后對每一個字符數(shù)組的元素進行正則匹配,含有數(shù)字的單獨進行處理
public class SimpleString { // 打印字符串的函數(shù) 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]+"); // 根據(jù)正則表達式,刪除一個或多個空格,將字符串保存為字符數(shù)組 for (int i = 0; i < ss.length; i++) { // 將每一個字符數(shù)組的首字母改為大寫 String up = ("" + ss[i].charAt(0)).toUpperCase(); // 大寫 StringBuffer sb = new StringBuffer(ss[i]); ss[i] = sb.replace(0, 1, up).toString(); // 上邊已經(jīng)把字符串數(shù)組的首字母該為大寫,然后對更改后的字符數(shù)組判斷是否有數(shù)字 Matcher m = Pattern.compile("\\d+").matcher(ss[i]);// 0-9出現(xiàn)一次或多次 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 + "_"; // 數(shù)字前后都添加"_" 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算法之數(shù)組冒泡排序代碼實例講解
- Java算法實現(xiàn)調整數(shù)組順序使奇數(shù)位于偶數(shù)之前的講解
- Java算法實現(xiàn)楊輝三角的講解
- Java算法之冒泡排序實例代碼
- Java算法之最長公共子序列問題(LCS)實例分析
- java算法實現(xiàn)紅黑樹完整代碼示例
- Java算法之堆排序代碼示例
- java算法之二分查找法的實例詳解
- java算法導論之FloydWarshall算法實現(xiàn)代碼
- java算法實現(xiàn)預測雙色球中獎號碼
- Java算法之遞歸算法計算階乘
- JAVA算法起步之插入排序實例
- JAVA算法起步之堆排序實例
- JAVA算法起步之快速排序實例
- 關于各種排列組合java算法實現(xiàn)方法
- Java算法之時間復雜度和空間復雜度的概念和計算
相關文章
Java實現(xiàn)監(jiān)聽UDP協(xié)議的指定端口并收到數(shù)據(jù)按照十六進制輸出方式
這篇文章主要介紹了Java實現(xiàn)監(jiān)聽UDP協(xié)議的指定端口并收到數(shù)據(jù)按照十六進制輸出方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04Java listener簡介_動力節(jié)點Java學院整理
這篇文章主要介紹了Java listener簡介,可以用于統(tǒng)計用戶在線人數(shù)等,有興趣的可以了解一下2017-07-07