How do I extract a string from user input?
milage = atoi(strt开发者_C百科ok(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?
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
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().
精彩评论