21.串的处理

题目
21.串的处理_第1张图片

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();

		char[] c = str.toCharArray();
		int n = c.length;
		
    StringBuilder st = new StringBuilder();
		
    int i = 0;
		
    while(i<n) {
			char x = c[i];
			if(x>='a'&&x<='z') {
				if(i == 0 || c[i-1] == ' ') {
					x^=32;
				}
				st.append(x);
				i++;
			}else if(x==' ') {
				st.append(x);
				while(c[i] == ' ')
					i++;
			}else {
				if(i>0&&c[i-1]>='a'&&c[i-1]<='z')
					st.append('_');
				st.append(x);
				if(i+1<n&&c[i+1]>='a'&&c[i+1]<='z')
					st.append('_');
				i++;
			}
		}
		System.out.println(st);
		sc.close();
	}
}

你可能感兴趣的:(蓝桥杯省赛(Java组),java,开发语言)