题目描述
给定一个由多个命令字组成的命令字符串:
- 字符串长度小于等于127字节,只包含大小写字母,数字,下划线和偶数个双引号;
- 命令字之间以一个或多个下划线_进行分割;
- 可以通过两个双引号””来标识包含下划线_的命令字或空命令字(仅包含两个双引号的命令字),双引号不会在命令字内部出现;
请对指定索引的敏感字段进行加密,替换为******(6个*),并删除命令字前后多余的下划线_。 如果无法找到指定索引的命令字,输出字符串ERROR。
输入描述
输入为两行,第一行为命令字索引K(从0开始),第二行为命令字符串S。
输出描述
输出处理后的命令字符串,如果无法找到指定索引的命令字,输出字符串ERROR
用例1
输入
1
password__a12345678_timeout_100
输出
password_******_timeout_100
用例2
输入
2
aaa_password_"a12_45678"_timeout__100_""_
输出
aaa_password_******_timeout_100_""
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = Integer.valueOf(in.nextLine());
String str = in.nextLine();
StringBuilder builder = new StringBuilder();
Boolean tag = false;
int index = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '"') {
tag = !tag;
}
if (index == num) {
builder.append("******");
if (tag) {
i++;
while (str.charAt(i) != '"') {
i++;
}
tag = !tag;
i++;
} else {
while (str.charAt(i) != '_' && tag) {
i++;
}
}
}
if (i < str.length() && str.charAt(i) == '_') {
if (!tag) {
index++;
}
while ((i + 1) < str.length() && str.charAt(i + 1) == '_' ) {
i++;
}
}
builder.append(str.charAt(i));
}
while (builder.toString().endsWith("_")) {
builder = new StringBuilder(builder.substring(0, builder.length() - 1));
}
System.out.println(builder.toString());
}
}