Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言编写的简单高中水平字符串操作任务_C_String - Fatal编程技术网

用C语言编写的简单高中水平字符串操作任务

用C语言编写的简单高中水平字符串操作任务,c,string,C,String,我很好奇你将如何完成这项任务: 输入字符串max 101 char。输入两个数字n,m=

我很好奇你将如何完成这项任务:

输入字符串max 101 char。输入两个数字n,m=<100和n 程序以在每个字母或数字之间添加星号*的方式输出输入的字符串。然后输出相同的字符串,但它会删除n和m字符之间的星号,并对字符串中n和m个定位字符之间的字符串进行排序

例如:

输入:

sparta
2, 5
输出:

s*p*a*r*t*a
s*aprt*a

应使用基本功能。谢谢

我会这样做:

int main(int argc, const char *argv[])
{
    const char *input = argv[1];
    int inputLen = strlen(input);
    int index1 = atoi(argv[2]) - 1;
    int index2 = atoi(argv[3]) + 1;

    char *output = malloc(inputLen * 2);
    int outputLen = inputLen * 2;

    for (int i = 0; i < outputLen; i += 2)
    {
        output[i] = input[i / 2];
        output[i + 1] = '*';
    }

    // end the string
    output[outputLen - 1] = '\0';
    puts(output);

    char *output2 = malloc(outputLen + 1);

    index1 = (index1 - 1) * 2;
    index2 = (index2 - 1) * 2;

    int output2index = 0;

    for (int i = 0; output[i] != '\0'; i++) {
        // the problem is that these characters are 
        // representative of the original positions in the string
        if (!(i >= index1 && i <= index2 && output[i] == '*'))
        {
            output2[output2index++] = output[i];                
        }
    }

    output2[output2index] = '\0';
    puts(output2);

    free(output);
    free(output2);
}

毫无疑问?仅仅是单个示例的预期输出?你试过什么?我可以帮你编码。我每小时收费40美元,所有计费时间四舍五入到最近的一小时,第一个小时预付。你有Paypal吗?家庭作业标签并不意味着你可以在不解释你尝试了什么的情况下提问。实际上恰恰相反,对你所做的解释为解释错误提供了一个更好的起点,因此这个人更有可能会学到一些东西。@StealthRabbi它没有显示整个帖子:我编辑了它。@JackManey,你太谦虚了,每小时40美元,有人可能会接受你。我想说,在评论中发布代码至少300美元/小时是行不通的。请把你的代码放进去