简体   繁体   中英

How to insert a copy of a string into another string?

So, here is the function:

int strinsert(char *dst, int len, const char *src, int offset)

I need to insert a copy of my src string into the string called dst from the position offset . The argument len specifies the number of characters reserved for the array dst .

the important part of the code:

int strinsert(char *dst, int len, const char *src, int offset)
{
    strncpy(dst, src+offset, len);
    char buf[100];
    strcpy(buf+len, src);
    len += strlen(src) ;
    strcpy(buf+len, dst+offset); 
    strcpy(dst, buf);
    
    return 1;
}

Still feels kind of off...

Edit: Before someone misunderstood, I am just teaching myself how to program in C and I found this exercise. Btw, I didn't really found some good learning material for one- and two-dimensional arrays, could someone be so kind and post some?

Does this solve the problem?

int strinsert(char *dst, int len, const char *src, int offset) 
{
    char temp [MAX_SIZE];

    strncpy(temp, dest+offset, strlen(dest+offset));
    strncpy(dest+offset, src, strlen(src));
    strcat (dest+offset+strlen(src), temp);
    
    return 0;
    
}

Obviously, the above code doesn't have any error checking, and the temp can be malloced etc.

You can use strncpy() , which is a function of string.h library. It copies first num characters of source string to destination string.

char * strncpy ( char * destination, const char * source, size_t num );

But in your case, you need a third string because you can not expand your destination array. You can copy substring (from beginning to offset) of destination array into this third array and concatenate the rest. So you need to do something like this:

char *dst = "ThisIsMyHomework";
char *src = "Not";
char finalString[50];
int offset = 6; //Assume we want to insert "Not" string between "ThisIs" and "MyHomework". So we want src array to start from 6th index of dst array.

strncpy(finalString, dst, offset); //finalString="ThisIs"
finalString[offset] = '\0'; //For finalString, you have to add "\0" manually because strcat will append other strings from 50. index of array if you don't
strcat(finalString, src); //finalString="ThisIsNot"
strcat(finalString, dst + offset); //finalString="ThisIsNotMyHomework"

Its a bit painful, but you really have to contruct a new string as you can't really shuffle bits of memory around so easily and I don't think there is a library function to do this (is there??). Somthing like this:

int strinsert(char *dst, int len, const char *src, int offset)
{
    char *new_string = new char[len];
    int remaining = len;

    // Check offset is not to long (+1 for null)
    if (offset >= remaining)
        offset = remaining;

    // copy the pre-string from dest
    strncpy(new_string, dest, offset);
    // Calulate the remaining space
    remaining -= offset;

    // Add the insert string (with max chars remaining)
    strncat(new_string, src, remaining);
    // calc remaining space
    remaining -= strlen(src);

    // Add the post-string from dest (with max chars remaining)
    strncat(new_string, dest, remaining);

    // Finally copy the new_string into dest
    strncpy(dest, new_string, len);

    // free the memory
    delete [] new_string;
}

Note: You probably need to do a better job of calculating the remaining space incase it goes negative...

Edit: replaced variable length array (illegal ... oops) with mem allocation

void strinsert(char *dst, size_t len, const char *src, size_t offset) 
{
  size_t iLenDst = strlen(dst),
         iLenSrc = strlen(src);

  // Some error handling
  if (iLenDst+iLenSrc+1>len)  
  {
    ASSERT(FALSE):
    return;
  }

  // restrict to max length
  if (offset>iLenDst)
    offset = iLenDst;

  // Make room incl. trailing \0
  memmove(dst+offset+iLenSrc,dst+offset,iLenDst-offset+1);
  // Insert new
  memcopy(dst+offset,src,iLenSrc);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM