我很好奇你将如何完成这项任务:
输入一个字符串(最多101个字符)。输入两个数字n,m =< 100和n
程序以在每个字母(或数字)之间添加星号(*)的方式输出输入的字符串。然后输出相同的字符串,但它删除n和m char之间的星号,并在字符串中对n和m位置字符之间的字符串进行排序。
示例:
输入:
sparta
2, 5
输出:
s*p*a*r*t*a
s*aprt*a
应使用基本功能。谢谢!
答案 0 :(得分:1)
我就是这样做的:
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);
}