首页 > 其他 > 详细

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

时间:2020-01-16 15:44:02      阅读:81      评论:0      收藏:0      [点我收藏+]

题目描述
?连续输入字符串,请按长度为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;
}

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

原文:https://blog.51cto.com/13614527/2467260

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!