Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

String insertion into another string

// Insert q char c in pos n of the string str.
int strinsert(char **str, char c, int  n, int q)
{
    int len = my_strlen(*str);
    char ins[q + 1];
    char *new = malloc(len + q + 1);

    memset(ins, c, q);
    memset(new, 0, len + q);
    ins[q] = '';
    strncpy(new, *str, n);
    new[n] = '';
    strcat(new, ins);
    strcat(new, *str + n);
    return (0);
}

strinsert(&str, 'X', strlen(str) / 2, 5);
// Inserting in string str 5 'X' in the middle of the string
 
PREVIOUS NEXT
Tagged: #String #insertion #string
ADD COMMENT
Topic
Name
2+8 =