Link to home
Start Free TrialLog in
Avatar of uri22
uri22

asked on

Parsing string function

I need a function that does the follows.
It receives as parameters :
1.string
2.delimiter
3.structure
int parse(char *string, char *delimiter, struct struct)
The function shell parse the string which consists data and delimiters, and transfer the data to the struct members.
The function shell check weather the number of parameters (after parsing the message) is equal to the number of members in the structures and return a proper error if neccessary.
e.g.
struct ur{
char param1
char param2
char param3
}
string : "123,USD,abc,"
delimiter : ,
in this case the outcome of the function will be:
param1=123
param2=USD
param3=abc

Need a working function as an answer
Thanks
uri22
Avatar of s_andrew
s_andrew

Hi,

Do you want any arbitrary structure from your program to be filled with this function? If that's what you want you will probably fail to do that - this will require dynamic type detection, which is not available in C.

In any case your example is wrong as well - you will fail to fill 'char' struct member paramX with strings (or numbers?) "123" or "USD" - you should use char* or char[XX] to store that.
sounds like homework, so no-one will post code (anyone who does is at risk of having his account cancelled.

As a hint, however, you need to use strtok()  Look in your help files. Call strtok() in a loop. That's it really, nothing difficult.

btw, if it's not homework & your parsing a program'S command line, look into getopt()
Avatar of uri22

ASKER

graham_k
BTW, THIS IS NOT HOMEWORK !!!!!!!
It is a part of a very big project I'm working on right now.
I thought I can use some help from the experts.
The name of this site is experts exchange, isn't it what it meant for?
Thanks.
uri22
Are you still looking for an answer?
ASKER CERTIFIED SOLUTION
Avatar of Triskelion
Triskelion
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Force/accepted by

Netminder
Community Support Moderator
Experts Exchange