zoukankan      html  css  js  c++  java
  • 字符串分割

    题目描述

    连续输入字符串(输出次数为N,字符串长度小于100),请按长度为8拆分每个字符串后输出到新的字符串数组,

    长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

    首先输入一个整数,为要输入的字符串个数。

    例如:

    输入:2

    abc

    12345789

    输出:abc00000

    12345678

    90000000

    接口函数设计如下:

    /*****************************************************************************
    功能:存储输入的字符串

    输入:字符串

    输出:无

    返回:0表示成功,其它返回-1
    ******************************************************************************/

    int  AddString(char *strValue);
    /****************************************************************************
    功能:获取补位后的二维数组的长度

    输入:无

    输出:无

    返回:二维数组长度
    *****************************************************************************/

    int  GetLength();


    /*****************************************************************************
    功能:将补位后的二维数组,与输入的二维数组做比较

    输入:strInput:输入二维数组,iLen:输入的二维数组的长度

    输出:无

    返回:若相等,返回0;不相等,返回-1.其它:-1;
    ******************************************************************************/
    int  ArrCmp(char strInput[][9],int iLen);

    输入描述:

    首先输入数字n,表示要输入多少个字符串。连续输入字符串(输出次数为N,字符串长度小于100)。

    输出描述:

    按长度为8拆分每个字符串后输出到新的字符串数组,长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

    示例1

    输入

    2
    abc
    123456789
    

    输出

    abc00000
    12345678
    90000000

    思路:(多加的while是因为做题后台输入需要测试多组)输入字符串,判断字符串是否能被8整除,不能的话,看还差多少能被8整除,字符串结尾多加多少0,知直到能被8整除,然后利用for循环切割字符串

    其他人代码:
    import java.util.Scanner;public class Main{
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){
                int n = Integer.valueOf(sc.nextLine());
                for (int i = 0; i <n; i++){
                    String str = sc.nextLine();
                    helper(str);
                }
            }
        }
        private static void helper(String s){
            if (s == null) return;
            int a = 0;
            if (s.length() % 8 != 0) a = 8 - s.length()%8;
            while(a>0) {
                s += "0";
                a--;
            }
            for (int i = 0; i < s.length(); i+=8){
                System.out.println(s.substring(i, i+8));
            }
        }
    }
  • 相关阅读:
    *****.NET程序首次加載慢問題
    *****How to access a repositoryItemButtonEdit value from a FileDlg
    抽象类与接口的区别
    C#中使用Split分隔字符串的技巧
    *****User Control Instance/DataGridView.Columns in Visual Studio Design
    LinksPersistInfo not work!
    LookUpEdit用法
    Devpress.XtraGrid.GridControl 笔记
    *****动态执行sql
    *****LookUpEdit hide columns
  • 原文地址:https://www.cnblogs.com/hisoka-study/p/13473708.html
Copyright © 2011-2022 走看看