【华为机试练习】字符串分隔

题目描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(输入2次,每个字符串长度小于100)
输出描述:
输出到长度为8的新字符串数组


解法(C语言版):

#include<stdio.h>
#include<string.h>

char *outputstr(char *in, char *out)
{
    int i;
    for(i = 0; i < 8; ++i)
    {
        if(in[i] != NULL)
            out[i] = in[i];
        else
            out[i] = '0';
    }
    return out;
}

int main()
{
    char instr[100] = {0};
    char outstr[9] = {0};
    while(gets(instr))
    {
        char *p = instr;
        while(*p != '\0')
        {
            printf("%s\n", outputstr(p, outstr));
            p += 8;
        }
        memset(instr, 0, 100);
    }
    return 0;
}

猜你喜欢

转载自blog.51cto.com/13614527/2467260