tags:

views:

48

answers:

1

input as;

(A%v),(c+e),-p#-d //include no white space

i want to hold input as

A[0]=A%v
A[1]=c+e
A[2]=-p#-d

is it possible? how?`

+4  A: 

If you are in C, this is possible with strtok. The signature of strtok is

char *strtok( char *str1, const char *str2 );

where str2 is the delimiter you want to split str based on. In this case, it would be ",". The first call needs a real string for str1, but subsequent calls to get the next tokens should have NULL there.

Here is a simple loop for you:

// myString is your input char *
char *token = strtok(myString, ",");
int index = 0;
while(token != NULL)
{
   // put token in an array here at [index++], but be careful
   // and make sure the array has enough space allocated

   // before the loop ends, grab the next token
   token = strtok(NULL, ","); // don't pass in another string! Use NULL
}
// now you have all of the tokens in your array, hopefully

I should add that this gets you most of the way there, but you should make sure your array has enough space allocated to it! You could count the number of , characters in the string beforehand and malloc that many char *s, for example. And you need to copy the tokens over using strncpy - don't just point each element in the array to the token you get back.

Phil