華為機試:字串分割
阿新 • • 發佈:2021-06-19
描述
•連續輸入字串,請按長度為8拆分每個字串後輸出到新的字串陣列;
•長度不是8整數倍的字串請在後面補數字0,空字串不處理。
輸入描述:
連續輸入字串(輸入多次,每個字串長度小於100)
輸出描述:
輸出到長度為8的新字串陣列
輸入:
abc
123456789
輸出:
abc00000
12345678
90000000
程式碼:
package huawei_jishi; import java.util.Scanner; /** * author:沒拉鍊的布加拉提 */ public class StringSplit8 { public static void main(String[] args) { Scanner sc= new Scanner(System.in); while (sc.hasNext()) { String st = sc.nextLine(); int n = st.length(); int nn = n; int m = 0; while (n>8) { System.out.println(st.substring(m, m+8)); m += 8; n-= 8; } System.out.println(st.substring(nn-n, nn)+new String(new char[8-n]).replace("\0", "0")); } sc.close(); } }