this is an example of what I wanna do I have the following list
[token]
[token1]
[token22]
[token99]
[8token]
[3token3]
and I wanna remove "[" and "]" then replace the word token with my own word. so I have the following code:
char *getToken(char *repl, char *mysrc)
{
const char *p1 = strstr(mysrc,"[")+1;
const char *p2 = strstr(p1,"]");
size_t len = p2-p1;
char *src = (char*)malloc(sizeof(char)*(len+1));
strncpy(src,p1,len);
src[len] = '\0';
char *find = "token";
char *found;
if(strcmp(src,find) == 0)
return src;
char *res = malloc(strlen(src) + strlen(repl) + 1);
if (res == NULL)
return src;
found = strstr(src, find);
/* Search string not found, return the whole of the source */
if (found == NULL){
strcpy(res, src);
return res;
}
/* Paste the replacement string in */
strncpy(res, src, (size_t)(found - src));
strcat(res, repl);
strcat(res, found + strlen(find));
free(src);
return res;
}
which is working fine except that in some situations I get an X or H in front of the result. like this: Xtest22 instead of test22
Did I do something wrong with strlen? I can't seem to find out where I'm doing wrong.