Hi,
The
strtok() function takes two arguments: the first one is the string that you want to break up, and next the delimiter string i.e. a string containing all the weird characters based on which you want to break up the first argument.Here it is:
Code: Select all
#include<string.h>
char*strtok(char*string, constchar*delim);
If
string is not NULL, the function scans
string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.
Note that delimiters may vary from a call to another.
So your problem might be solved as:
Code: Select all
/* strtok example */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char str[] ="2.7183 3.1416 5.142";
char * pch;
double f;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
f = atof(pch);
printf ("%.4lf\n",atof(pch));
pch = strtok (NULL, " ");
}
return 0;
}
Regards,
Suman.
P.S:
Do not use
Code: Select all
char *str ="2.7183 3.1416 5.142";
Strtok will try to alter the contents of what the compiler stores as a read-only string and hence invoke undefined behaviour.