If you are using a POSIX compliant system strtok() is the function to use for
the kind of
operation you have mentioned.
char *strtok(char *s, const char *delim);
It will create the tokens for you . You have to call it in a loop to get all
the tokens.
After the first token is created you need to call strtok with argument delim=
NULL .
In your example :
For the first time you have to call
strtok(mywords,"'")
and then
strtok(mywords,NULL), subsequently until there are no delimiters left.
Do a "man strtok" or a web search to see the documentation.
Regards,
-Saswat
On Thu, 2007-02-15 at 04:55 +0000, Hermann wrote:
> My Code below won't work,
> I would like to split strings delimited by commas and put them into
> arrays;
> Anyone can fix my code?
> Thank you.
>
> #include <stdio.h>
> #include <string.h>
>
> int main()
> {
> char *mywords;
> unsigned int i,a;
> char *myarray;
>
> myarray = "\0";
> mywords = "Hallo,Hi,Hai,Hey";
>
> for(a=0;a<=3;a++){
> for(i=0;i<=strlen(mywords)-1;i++){
> if(mywords[i]!=','){
> myarray[a] = strcat(myarray[a],mywords[i]);
> }
> }
> }
> printf("%s\n",myarray[0]);
> printf("%s\n",myarray[1]);
> printf("%s\n",myarray[2]);
> printf("%s\n",myarray[3]);
> }
>
>
>
>
>
>