public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while ((n--) > 0) { StringBuffer sb = new StringBuffer(sc.next()); int lenOld = sb.length(); int lenNew = lenOld; for (int i = 1; i < lenOld; i++) { if (Character.isLowerCase(sb.charAt(i - 1)) && Character.isUpperCase(sb.charAt(i))) { lenNew++; } if (i < lenOld-1 && Character.isLowerCase(sb.charAt(i+1)) && Character.isUpperCase(sb.charAt(i - 1)) && Character.isUpperCase(sb.charAt(i))) { lenNew++; } } int indexOld = lenOld - 1; int indexNew = lenNew - 1; sb.setLength(lenNew); for (; indexOld >= 1 && indexOld < lenNew; indexOld--) { if (Character.isLowerCase(sb.charAt(indexOld - 1)) && Character.isUpperCase(sb.charAt(indexOld))) { sb.setCharAt(indexNew--, sb.charAt(indexOld)); sb.setCharAt(indexNew--, '_'); } else if (indexOld < lenNew - 1 && Character.isLowerCase(sb.charAt(indexOld + 1)) && Character.isUpperCase(sb.charAt(indexOld - 1)) && Character.isUpperCase(sb.charAt(indexOld))) { sb.setCharAt(indexNew--, sb.charAt(indexOld)); sb.setCharAt(indexNew--, '_'); } else { sb.setCharAt(indexNew--, sb.charAt(indexOld)); } } System.out.println(sb.toString().toLowerCase()); } }
点赞 评论

相关推荐

10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
牛客网
牛客企业服务