L.Rat
L.Rat

Reputation: 39

How to combine a strings and store it in a array after using strtok() from a "full string" in C

Im a new learner of C and i got trouble combining the strings which separated by strtok(). Using Clion (C99) Lets have a look at my codes.

char recipient[30];
char final_destination[50];
char status[10];


printf("Please enter 1> Recipient-, 2> Final Destination- and 
3>Delivery status :\n");    Entered(L-Rat Kitchen-House Not-Arrived)

scanf("%29s%49s%9s", recipient, final_destination, status);

///only recipient first///The purpose of storing "recipient final_destination, status" in a string is because to let user input data in a same line while using scanf .After that, i use strtok to separate those words before/after "-" .Separate different string by using the spacebar in scanf which prevents data input like (L-ratKitchenHouseNot-arrive).

const char s[] = "-";
char *token;
token = strtok(recipient, s);
while( token != NULL )
 ???????????????????????

token = strtok(NULL, s);

At the line of ?????????????? , im stuck , i dont know how or is there a way to put those processed data in a array and combine it.

Expecting:

Input: (L-Rat House-Kitchen Not-Arrive)

Output: (L Rat House Kitchen Not Arrive)

Would be very helpful if theres a solution !!!

Upvotes: 0

Views: 248

Answers (1)

akshay kishore
akshay kishore

Reputation: 1027

try this, if you want store tokens in an array.

while (token != NULL) 
    { 
        printf("%s\n", token); 
        token = strtok(NULL, "-"); 
    }

Upvotes: 1

Related Questions