[ create a new paste ] login | about

Link: http://codepad.org/Z2bL5vPx    [ raw code | fork ]

C, pasted on Sep 18:
#include <stdio.h>
#include <string.h>

// 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 0
    // or you could use malloc() to allocate sufficient space

    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
    // deallocate buf[] here, if used malloc()
}

int main() {
    char string[100] = "ABCDE";
    append(string, "ZZ", 3);
    puts(string);

    return 0;
}


Create a new paste based on this one


Comments: