输入一个由小写字母组成的字符串, 按照26个字母表顺序进行排序,打印排序后的字符串;

如输入 bdca 输出 abcd

第一种算法:

public class Test03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一串字符串:");
		String str = sc.nextLine();
		char[] a = str.toCharArray();
		for (int i = 0; i < a.length - 1; i++) {
			for (int j = i + 1; j < a.length; j++) {
				if (a[i] > a[j]) {
					char temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
		}
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "\t");
		}
	}

}

第二种算法:

public class Test03 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一串字符串:");
		String str = sc.nextLine();
		char[] a = str.toCharArray();
                Arrays.sort(a);
                System.out.println(new String(a));
        }
}

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页