I have this string:
char *path = " one\\\two\\\five\\\six";
I want to separate it to an array when every cell is the string.
I’m using strtok, buy my problem is in calculating how much space to allocate for the array.
I’m doing that:
for ( i = 0 ; i < strlen(path) ; i++)
{
if (path[i] == '\' && path[i+1] == '\') /*doesn't work! :( */
counter++
}
the comparator line isn’t working… I also tried writing : “\” but it doesn’t work as well.
What is wrong?
editing
I’m trying to use strtok and It get’s messy 🙁
char seps[] = "\\";
char *elemName = NULL;
elemName = strtok( path, seps );
while( elemName != NULL )
{
/* While there are tokens in "string" */
/*copy the elemName*/
stringArr[runner] = strdup(elemName);
/* Get next token: */
elemName = strtok( NULL, seps );
}
printf("printing the strings!!!!\n");
for ( i = 0 ; i < arr_size ; i++)
{
printf (" %s \n " , stringArr[i]);
}
you need to write
'\\'(or numeric 92, as it’s\ASCII value).\is an escape character, and if you want to have it included in string, you need to escape it on it’s own.explanation:
\(whatever)is translated to one char in compilation phase. (for example,\n-> char with ASCII value 0x0A)