How is it that a string, or specifically a part of a string, can be isolated and put into its own integer variable? For example, a user is asked to input a date, such as 12/31/2000, which is saved as a string. How can the "12" the "31" and the "2000" be isolated and saved as individual integer variables?
TimCereja 0 Newbie Poster
Recommended Answers
Jump to PostLook at the characters.
If there is a digit, subtract '0' to make it a number and accumulate it into a variable -- details for you to work out.
If you get a /, next digits go into another variable.
Jump to PostLook up the library function strtol, it will convert the digits ignoring leading blank spaces and pass a pointer back to where it finished, which will be the '/', you can then just hop over the '/' to convert the next piece of the number.
Jump to Postyou can use strtol() which avoids at least one of the problems with strtok().
Jump to PostWe can also use atoi() function for converting string to integer....
like
int_value = (unsigned int)atoi(string_val);
wherestring_val is
char * string_val;
Jump to Posti don't recommend atoi() or atol(). this does not handle invalid strings (non-integer strings) very well at all. for instance, if you try
value = atoi("CowAndChicken");
the result will be zero (value = 0)
and if you try
value = atoi("0");
the result …
All 21 Replies
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Banfa 597 Posting Pro Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
sree_ec 10 Junior Poster
jephthah 1,888 Posting Maven
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
jephthah 1,888 Posting Maven
Banfa 597 Posting Pro Featured Poster
jephthah 1,888 Posting Maven
Banfa 597 Posting Pro Featured Poster
jephthah 1,888 Posting Maven
TimCereja 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
TimCereja 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
TimCereja 0 Newbie Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
TimCereja 0 Newbie Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
TimCereja 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.