How can I copy strings in C so that do not overlap the old values? I’d used strcpy() but it clean dest to set values of src.
char* foo = " This is my string \0";
char* new = malloc(strlen(str) + 1);
char* token;
int size = 0;
token = strtok(foo, " \t");
while( NULL != token )
{
int i;
for(i = 0; token[i] != '\0'; i++)
{
new[size++] = token[i];
}
new[size++] = ' ';
token = strtok(NULL, " \t");
}
new[size] = '\0';
I want an alternative for this block of code:
int i;
for(i = 0; token[i] != '\0'; i++)
{
new[size++] = token[i];
}
new[size++] = ' ';
There is an native method in C for do this? I not found any function in string.h. Thanks in advance.
How about strdup? It even allocates the target buffer for you.