将字符插入字符串

时间:2011-09-18 03:13:58

标签: c string

我想将""添加到{"status":true},以便字符串看起来像"{"status":"true"}"。如何在特定位置将字符插入字符串?

我尝试了strncat(),但无法获得所需的结果。我读到你需要为此创建自己的功能。有人能告诉我一个例子吗?

4 个答案:

答案 0 :(得分:4)

是的,您需要为此编写自己的功能。

请注意,C中的字符串是char[],即字符数组,且大小固定。

你可以做的是,创建一个新的字符串作为结果,将主题字符串的第一部分复制到其中,追加中间的字符串,并附加主题字符串的后半部分。 / p>

代码类似于

// inserts into subject[] at position pos
void append(char subject[], const char insert[], int pos) {
    char buf[100] = {}; // 100 so that it's big enough. fill with zeros
    // or you could use malloc() to allocate sufficient space
    // e.g. char *buf = (char*)malloc(strlen(subject) + strlen(insert) + 2);
    // to fill with zeros: memset(buf, 0, 100);

    strncpy(buf, subject, pos); // copy at most first pos characters
    int len = strlen(buf);
    strcpy(buf+len, insert); // copy all of insert[] at the end
    len += strlen(insert);  // increase the length by length of insert[]
    strcpy(buf+len, subject+pos); // copy the rest

    strcpy(subject, buf);   // copy it back to subject
    // Note that subject[] must be big enough, or else segfault.
    // deallocate buf[] here, if used malloc()
    // e.g. free(buf);
}

Working example here

答案 1 :(得分:1)

使用sprintf()

const char *source = "{\"status\":\"true\"}";

/* find length of the source string */
int source_len = strlen(source);

/* find length of the new string */
int result_len = source_len + 2; /* 2 quotation marks */

/* allocate memory for the new string (including null-terminator) */
char *result = malloc((result_len + 1) * sizeof(char));

/* write and verify the string */
if (sprintf(result, "\"%s\"", source) != result_len) { /* handle error */ }

/* result == "\"{\"status\":\"true\"}\"" */

答案 2 :(得分:0)

我尝试了strinsert的实现并将其粘贴到下面。它编译并通过VS2010进行测试。

该函数采用指向目标缓冲区的指针,目标缓冲区大小,要插入的字符串以及插入字符串的位置。如果出现错误,该函数返回-1,否则返回目标缓冲区的大小。如果目标缓冲区太小而无法容纳插入的字符串,则使用realloc调整缓冲区的大小并返回新的缓冲区大小。

我使用memmove而不是strncpy,因为我认为当源和目标重叠时strncpy是未定义的。如果插入的字符串小于移动的内存量,则可以执行此操作。

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>

int strinsert(char **dest, size_t destsize, char *ins, size_t location)
{
    size_t origsize = 0;
    size_t resize = 0;
    size_t inssize = 0;

    if (!dest || !ins)
        return -1;  // invalid parameter

    if (strlen(ins) == 0)
        return -1; // invalid parameter

    origsize = strlen(*dest);
    inssize = strlen(ins);
    resize = strlen(*dest) + inssize + 1; // 1 for the null terminator

    if (location > origsize)
        return -1; // invalid location, out of original string

    // resize string to accommodate inserted string if necessary
    if (destsize < resize) 
        *dest = (char*)realloc(*dest, resize);

    // move string to make room for insertion
    memmove(&(*dest)[location+inssize], &(*dest)[location], origsize - location);
    (*dest)[origsize + inssize] = '\0'; // null terminate string

    // insert string
    memcpy(&(*dest)[location], ins, inssize);

    return max(destsize, resize); // return buffer size
}

void check(int retVal)
{
    if (retVal < 0)
    {
        assert(!"error code returned!\n");
        exit(1);
    }
}

#define STARTSTRING "Hello world!"

int _tmain(int argc, _TCHAR* argv[])
{
    // initialize str
    int bufsize = strlen(STARTSTRING) + 1 + 10; // added 1 for null terminator and 10 to test resize on demand
    int prevbufsize = 0;
    char *str = (char*)malloc(bufsize);
    strncpy_s(str, bufsize, STARTSTRING, strlen(STARTSTRING));
    printf("str = %s\n", str);

    // test inserting in the middle
    prevbufsize = bufsize;
    bufsize = strinsert(&str, bufsize, "awesome ", 6);
    assert(bufsize == prevbufsize); // this should not resize the buffer as it has room for 10 more bytes 
    check(bufsize);
    printf("str = %s\n", str);

    // test inserting at front
    prevbufsize = bufsize;
    bufsize = strinsert(&str, bufsize, "John says ", 0);
    assert(bufsize > prevbufsize); 
    check(bufsize);
    printf("str = %s\n", str);

    // test inserting char in the middle
    prevbufsize = bufsize;
    bufsize = strinsert(&str, bufsize, "\"", 10);
    assert(bufsize > prevbufsize); 
    check(bufsize);
    printf("str = %s\n", str);

    // test inserting char at end
    prevbufsize = bufsize;
    bufsize = strinsert(&str, bufsize, "\"", strlen(str));
    assert(bufsize > prevbufsize); 
    check(bufsize);
    printf("str = %s\n", str);

    free(str);
    return 0;
}

这是输出:

str = Hello world!
str = Hello awesome world!
str = John says Hello awesome world!
str = John says "Hello awesome world!
str = John says "Hello awesome world!"

答案 3 :(得分:0)

#include <stdio.h>
#include<stdlib.h>
#include<strings.h>


//{"status":true}\0_ _  
//{"status": true " } \0  

/*
 * parses the string, reallocates and shifts chars as needed, not generic though
 */

int
insertQuotes(char ** original, int sizeOriginal)
{
    int i = 0;
    char * start = NULL;
    char * temp = NULL;
    long lenLeft = 0;

    int newSize = sizeOriginal + 2;

    if(*original == NULL)
        return -1; 

    *original = realloc(*original, newSize);
    if (*original) {
        start = strstr(*original, ":");
        temp = *original + newSize - 1;
        *temp = '\0';
        temp--;
        *temp = '}';
        temp--;
        *temp = '"';
        temp--;

        while (temp > start + 1) {
            *(temp) = *(temp - 1);
            temp--;
        }
        *temp = '"'; 
        return 0;
    } else {
        return -1;
    }
}

int main()
{
    char * original = NULL;
    int retval = 0;

    original = (char *)malloc(sizeof("{\"status\":true}"));
    if (!original) return 1;    

    strncpy(original, "{\"status\":true}", sizeof("{\"status\":true}"));

    retval = insertQuotes(&original, sizeof("{\"status\":true}"));
    if (!retval) {
    printf("\nnew original is [%s]", original);
   }
   free(original);
   return 0;
}