题目描述
将一个字符中所有出现的数字前后加上符号“*”,其他字符保持不变
public static String MarkNum(String pInStr) {
return null;
}
输入描述
输入一个字符串
输出描述
字符中所有出现的数字前后加上符号“*”,其他字符保持不变
输入例子
Jkdi234klowe90a3
输出例子
Jkdi*234*klowe*90*a*3*
算法实现
import java.util.Scanner;
/**
* Author: 王俊超
* Date: 2015-12-25 17:04
* All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String input = scanner.nextLine();
System.out.println(markNumber(input));
}
scanner.close();
}
private static String markNumber(String s) {
boolean marked = false;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!marked && c >= '0' && c <= '9') {
builder.append('*');
marked = true;
} if (i > 0 && marked && (c< '0' || c > '9')) {
builder.append('*');
marked = false;
}
builder.append(c);
}
if (marked) {
builder.append('*');
}
return builder.toString();
}
}