gpt4 book ai didi

c - C语言如何在一个字符串中插入多个字符

转载 作者:行者123 更新时间:2023-12-02 00:16:34 25 4
gpt4 key购买 nike

我想在 C: 中的字符串中插入一些字符

示例:char string[100] = "20120910T090000";

我想让它像 "2012-09-10-T-0900-00"

到目前为止我的代码:

void append(char subject[],char insert[], int pos) {
char buf[100];

strncpy(buf, subject, pos);

int len = strlen(buf);

strcpy(buf+len, insert);

len += strlen(insert);

strcpy(buf+len, subject+pos);
strcpy(subject, buf);
}

当我第一次调用它时,我得到:2012-0910T090000

然而,当我第二次调用它时,我得到:2012-0910T090000-10T090000

感谢任何帮助

最佳答案

这是一些工作代码,它给出了输出:

String: <<20120910T090000>>
String: <<2012-0910T090000>>
String: <<2012-09-10T090000>>
String: <<2012-09-10-T090000>>
String: <<2012-09-10-T-090000>>
String: <<2012-09-10-T-0900-00>>

它使用 memmove() 因为它保证被复制的字符串周围重叠。

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

static void insert(char *str, size_t len, char c, size_t pos)
{
memmove(&str[pos+1], &str[pos], len - pos + 1);
str[pos] = c;
}

int main(void)
{
char string[25] = "20120910T090000";
// I want to make it something like "2012-09-10-T-0900-00"
char inschr[] = "-----";
int inspos[] = { 4, 7, 10, 12, 17 };
enum { NUMCHR = sizeof(inschr) / sizeof(inschr[0]) };
enum { NUMPOS = sizeof(inspos) / sizeof(inspos[0]) };
assert(NUMCHR == NUMPOS + 1);
size_t length = strlen(string);

printf("String: <<%s>>\n", string);
for (int i = 0; i < NUMPOS; i++)
{
insert(string, length, inschr[i], inspos[i]);
length++;
printf("String: <<%s>>\n", string);
}
return(0);
}

当然,我假设 C99 支持 for 循环符号。还要注意,在经典的 C 风格中,代码不采用目标字符串的大小,因此不能确保没有缓冲区溢出。添加参数并进行检查并不难;问题区域是如何指示函数失败。您可以使用该函数的不同接口(interface),将任意长度的字符串插入任意位置;它并没有变得更难......

String: <<20120910T090000>>
String: <<2012-0910T090000>>
String: <<2012-09-10T090000>>
String: <<2012-09-10-T090000>>
String: <<2012-09-10-T-090000>>
String: <<2012-09-10-T-0900-00>>

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

static int insert(char *str, size_t max, size_t len, char *ins, size_t pos)
{
assert(str[len] == '\0');
assert(len < max);
size_t inslen = strlen(ins);
if (len + inslen + 1 >= max)
return -1;
memmove(&str[pos+inslen], &str[pos], len - pos + inslen);
memmove(&str[pos], ins, inslen);
return len + inslen;
}

int main(void)
{
char string[25] = "20120910T090000";
// I want to make it something like "2012-09-10-T-0900-00"
char *insstr[] = { "-", "-", "-", "-", "-" };
int inspos[] = { 4, 7, 10, 12, 17 };
enum { NUMSTR = sizeof(insstr) / sizeof(insstr[0]) };
enum { NUMPOS = sizeof(inspos) / sizeof(inspos[0]) };
size_t length = strlen(string);
assert(NUMSTR == NUMPOS);

printf("String: <<%s>>\n", string);
for (int i = 0; i < NUMPOS; i++)
{
int newlen = insert(string, sizeof(string), length, insstr[i], inspos[i]);
if (newlen < 0)
{
printf("Oops! failed to insert [%s] into [%s]\n", insstr[i], string);
break;
}
length = newlen;
printf("String: <<%s>>\n", string);
}
return(0);
}

关于c - C语言如何在一个字符串中插入多个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12739176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com