I want to split a string with "." separator in C. For example, I have a string like this "studentdetails.txt". Now I want to result like this "studentdetails" and "txt". Please give me any idea to do it.
Asked
Active
Viewed 594 times
2 Answers
1
You may know about strtok in C.
Ex.
char str[] = "studentdetails.txt";
char delims[] = ".";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "%s\n", result );
result = strtok( NULL, delims );
}

Jayesh Bhoi
- 24,694
- 15
- 58
- 73
0
You can use a strtok() function.
char str[] ="This is a sample string, just testing.";
char *p;
printf ("Split \"%s\" in tokens:\n", str);
p = strtok (str," ");
while (p != NULL)
{
printf ("%s\n", p);
p = strtok (NULL, " ,");
}
return 0;
I have used a space... just use "." instead

Jay Nirgudkar
- 426
- 4
- 18
-
better use `strtok_r()` for safety (and readability) reasons. – The Paramagnetic Croissant Jun 28 '14 at 06:55
-
2The word is you rather then u. If it's worth writing an answer then it's worth using real words rather than text speak. – David Heffernan Jun 28 '14 at 07:39
-
The word is "than" and not "then" in your first sentence. If it's worth writing a comment, then its worth using proper grammar than not using one. sorry couldn't help myself. Cheers. :) – Jay Nirgudkar Jun 28 '14 at 07:44