如何在C中的字符串中插入多个字符

时间:2012-10-05 03:44:25

标签: c string

我希望在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

感谢任何帮助

2 个答案:

答案 0 :(得分:2)

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

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);
}

当然,我假设使用for循环表示法支持C99。另请注意,在经典C样式中,代码不会占用目标字符串的大小,因此不能确保没有缓冲区溢出。添加参数并进行检查并不是那么难;问题区域是如何表明功能失败。您可以使用与该函数不同的接口,将任意长度的字符串插入任意位置;这不是很难......

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);
}

答案 1 :(得分:1)

你可以这样做:

  char src[100] = "20120910T090000";
  char mask[] = "NNNN-NN-NN-C-NN-NN-NN";
  char dest[100] = {0};

  for (size_t s = 0,m = 0, d = 0; s < strlen(src) && m < strlen(mask); ++m)
  {
    switch (mask[m])
    {
    case 'N':
      assert(isdigit(src[s]));
      dest[d++] = src[s++];
      break;
    case 'C':
      assert(isalpha(src[s]));
      dest[d++] = src[s++];
      break;
    default:
      dest[d++] = mask[m];
      break;
    }
  }