bubdada
bubdada

Reputation: 343

How do I extract a string from user input?

milage = atoi(strtok(NULL, " "));
drive(&cars[carID-1], milage);

I have something like this for numbers, I want to use same thing for a name (character).

I tried this:

user = strtok(NULL, " ");
rent(&cars[carID-1], user);

but it did not work out.

Can any one help?

Upvotes: 1

Views: 212

Answers (2)

Joshua
Joshua

Reputation: 43270

char * is not a string type. char * is a pointer to byte array. Byte array is the string type.

Strings can be copied with strdup(). Copied strings need to be freed with free().

Upvotes: 0

hugomg
hugomg

Reputation: 69944

Are you just trying to extract numbers / strings from within another string? If that is the case you should probably have a look at sscanf. It works just like scanf but reads froms a string instead of from the standard input.

char name[100]; int mileage;
sscanf("username 42", "%s %d", name, &mileage);
//name now contains "username" and mileage now contains 42

Upvotes: 3

Related Questions