简体   繁体   中英

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?

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM