1. 程式人生 > >JAVA語言基礎——(五)字串(逆置)

JAVA語言基礎——(五)字串(逆置)

一、String下的方法

package practise;

import java.util.Arrays;
import java.util.Scanner;

public class StringMethod {

    public static String leftTurn(String str){

        return str;
    }

    public static void main(String[] args) {
        /*Scanner s = new Scanner(System.in);
        String str1 = s.nextLine();
//        System.out.println(str1);*/
        System.out.println("========.charAt(int)=========");
        String str2 = "helloworld";
        System.out.println("str2的長度:" + str2.length());
        System.out.println(str2.charAt(0));//h  返回字串該下標對應的字元
        System.out.println("========.codePointAt(int)=========");
        String str3 = "abcd";
        System.out.println(str3.codePointAt(0));//97    返回該下標對應字元的編碼
        System.out.println("=========.codePointBefore(int)========");
        System.out.println(str3.codePointBefore(1));//97    返回該下標的前一個字元的編碼
        System.out.println("=======.getChars(int,int,char[],int)==========");
        String str4 = "asdfgh";
        char[] arr = new char[6];
        char[] arr2 = new char[6];
        str4.getChars(0,5,arr,0);//將字串複製到一個字元陣列
        //str4.getChars(arr2,2);    報錯
        System.out.println(str4);
        System.out.println(arr);
        System.out.println("========.equals(String)=========");//將兩個字串進行比較
        String str5 = "qweert";
        String str6 = "qweert";
        String str7 = "qwerrt";
        System.out.println(str5.equals(str6));//true
        System.out.println(str5.equals(str7));//false
        System.out.println("===========.equalsIgnoreCase(String)===================");//不區分大小寫的比較兩個字串
        String str11 = "Qweert";
        System.out.println(str11.equalsIgnoreCase(str5));//true
        System.out.println("========.subString(int,int)=========");
        String str8 = "substring";
        String str9 = str8.substring(1,4);//將str8的1~4(不包括4)複製到str9  容易計運算元串的長度
        System.out.println(str9);
        System.out.println("========修改=========");
        String str10 = str8.substring(0,3) + "sub";//先提取需要的字元,在拼接上新新增的字元
        System.out.println(str10);
        System.out.println("========+拼接=========");
        String s = "hello";
        String s2 = "world";
        String s3 = s + s2;//按順序拼接
        System.out.println(s3);
        int a = 123;
        String s4 = s + a;//不是String型別,在拼接時也會轉換成字串型
        System.out.println(s4);
        System.out.println("===========.toCharArray()==============");
        String strr = "aaaaaaaaaa";//將字串轉換為字元陣列
        char[] ch = strr.toCharArray();
        System.out.println(Arrays.toString(ch));
        System.out.println("========.copyValueOf=============");//將數字元素複製成字串
        String strr2 = String.copyValueOf(ch);
        System.out.println(strr2);
        
    }
}

二、練習

1.字串左旋 如abcdefg——》cdefab

//左旋    abcdefg——》cdefab
package demo;

import java.util.Arrays;
import java.util.Scanner;

public class Demo1024 {
    public static char[] leftTurn(char[] array,int a){//左旋a個字元
        char tmp = 0;
        for(int i = 0;i < a / 2;i ++){//前a個元素逆序
            tmp = array[i];
            array[i] = array[a - 1 - i];
            array[a - 1 - i] = tmp;
        }
        for(int j = a;j < (array.length + a) / 2;j ++){//剩下的元素逆序
            tmp = array[j];
            array[j] = array[array.length - 1 + a - j];
            array[array.length - 1 + a - j] = tmp;
        }
        for(int k = 0;k < array.length / 2;k ++){//將整個陣列逆序
            tmp = array[k];
            array[k] = array[array.length - 1 - k];
            array[array.length - 1 - k] = tmp;
        }
        return array;
    }

    public static char[] toArray(String str){//字串轉化為字元陣列
        char[] arr = new char[str.length()];//新建一個長度為該字串長度的字元型陣列
        str.getChars(0,str.length(),arr,0);//將字串複製到陣列內
        //System.out.println(arr);
        return arr;
    }

    public static void main(String[] args) {
        System.out.println("請輸入一個字串:");
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
//        char[] arr1 = toArray(str);
//        System.out.println(arr1);
//        System.out.println(Arrays.toString(arr1));
        char[] arr = leftTurn(toArray(str),3);
//        System.out.println(Arrays.toString(arr));
        System.out.println(arr);
    }
}
package demo;

import java.util.Scanner;

public class Demo1026 {
    //將一個字串的某部分逆序
    public static String reverse(String str,int begin,int end){
        char[] ch = str.toCharArray();//字串轉換成陣列
        char tmp = 0;//中間變數
        while(begin < end){//開始和結束部分沒有相遇,進入迴圈
            //交換
            tmp = ch[begin];
            ch[begin] = ch[end];
            ch[end] = tmp;
            begin ++;//開始位置為下一個下標
            end --;//結束位置為前一個下標
        }
        return String.copyValueOf(ch);//返回字串
    }

    public static void leftRoadString(String str,int n){//左旋
        if(str ==null || n < 0 || n > str.length()){
            return;
        }
        int left = 0;
        int leftend = n - 1;
        int right = n;
        int rightend = str.length() - 1;
        str = reverse(str,left,leftend);
        str = reverse(str,right,rightend);
        str = reverse(str,left,rightend);
        System.out.println(str);
    }

    public static void main(String[] args) {
        System.out.println("輸入一個字串:");
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        System.out.println(str);
        leftRoadString(str,2);

    }
}

2.一句話中的單詞逆置,"Here is Tulun" ===> "Tulun is Here"

package demo;

import java.util.Scanner;

public class ReversrSentence {
    public static void reverse(char[] ch,int begin,int end){
        char tmp = 0;
        while(begin < end){
            tmp = ch[begin];
            ch[begin] = ch[end];
            ch[end] = tmp;
            begin ++;
            end --;
        }
    }

    public static String reverseSentence(String str){
        if(str == null){
            return null;
        }
        char[] ch = str.toCharArray();
        reverse(ch,0,ch.length - 1);//整體逆置
        int i = 0;//單詞開始
        int j = 0;//單詞結束
        while(i < ch.length){
            if(ch[j] == ' ' || j == str.length()){//j是空格,i~j的部分逆置
                reverse(ch,i,--j);
                i = ++j;
            }else if(ch[i] == ' '){//i是空格,i向後走作為下一個單詞的開始
                i ++;
                j ++;
            }else{//都不是空格,j向後走,尋找單詞的結束
                j ++;
            }
        }
        return String.copyValueOf(ch);

    }
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        System.out.println(reverseSentence(str));
    }
}