Is there a better/easier way to create a \w+
string from an existing string?
char *FixName(char *name)
{
char *ptr, tmp;
char *new = malloc(strlen(name)+1);
sprintf(new, "str_");
for (ptr = name + 4; *ptr; ptr++)
{
if ((*ptr >= '0' && *ptr <= '9') ||
(*ptr >= 'A' && *ptr <= 'Z') ||
(*ptr >= 'a' && *ptr <= 'z') ||
*ptr == '_')
{
tmp = *(ptr+1);
*(ptr+1) = '\0';
strcat(new, ptr);
*(ptr+1) = tmp;
}
}
free(name);
return new;
}
// USAGE:
//char *j = strdup("str_[](1.00)");
//printf("J: %s\n", j);
//j = FixName(j);
//printf("J: %s\n", j);
//free(j);
// OUTPUT:
//J: str_[](1.00)
//J: str_100
Thanks for comments, new function:
void FixName(char *name)
{
char *ptr;
unsigned short count = strlen("str_");
for (ptr = name + count; *ptr; ptr++)
{
if ((*ptr >= '0' && *ptr <= '9') ||
(*ptr >= 'A' && *ptr <= 'Z') ||
(*ptr >= 'a' && *ptr <= 'z') ||
*ptr == '_')
{
*(name+count) = *ptr;
count++;
}
}
*(name+count) = '\0';
}