Remove first two characters from char* or from int?

Y

yogi_bear_79

I have a char *year that contains a 4-digit year (i.e 1929), I use the
following syntax c = atoi(year); to convert it to an integer. However
I must strip the first two numbers. I've tried to convert it from
char* to a string and use the erase function, which works, but then I
can't seem to convert the string it to an integer. I am impartial as
to when I remove the first two digits, meaning it can be done before
or after the conversion to int. In my example above the end result
should be an integer variable equalling 29.
 
J

Jerry Coffin

I have a char *year that contains a 4-digit year (i.e 1929), I use the
following syntax c = atoi(year); to convert it to an integer. However
I must strip the first two numbers. I've tried to convert it from
char* to a string and use the erase function, which works, but then I
can't seem to convert the string it to an integer. I am impartial as
to when I remove the first two digits, meaning it can be done before
or after the conversion to int. In my example above the end result
should be an integer variable equalling 29.

c = atoi(year+2);
 
J

Jack Klein

I have a char *year that contains a 4-digit year (i.e 1929), I use the
following syntax c = atoi(year); to convert it to an integer. However
I must strip the first two numbers. I've tried to convert it from
char* to a string and use the erase function, which works, but then I
can't seem to convert the string it to an integer. I am impartial as
to when I remove the first two digits, meaning it can be done before
or after the conversion to int. In my example above the end result
should be an integer variable equalling 29.

First, it is not a good idea in either C or C++ code to use the ato...
functions from <stdlib.h> or <cstdlib>. They can cause undefined
behavior if the string represents a value too large for the return
type. It is better to use the strto... types, from the same headers.

See these examples of safer conversions, valid in both C and C++, if
you don't want to use strstreams or std::string for conversions.

http://jk-technology.com/c/code/strtol.html
http://jk-technology.com/c/code/getint.html

Using the strto... functions, or using a state machine like my other
example, or even using the less desirable atoi() function, there is a
very simple solution when using functions that accept a pointer to a C
string. Just add the number of characters to the pointer when you
pass it to the function.

Using atoi(), although I'd suggest you don't, look at the following
example valid in both C and C++:

#include <stdio.h>
#included <stdlib.h>

int main(void)
{
const char *year = "1929";
int full = atoi(year);
int part = atoi(year + 2);
printf("full year is %d and part year is %d\n", full, part);
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

Juha Nieminen

Perfect! Thanks!

Or alternatively: c = atoi(year) % 100;

(This will work even if there's whitespace at the beginning of the
string, or the year is already 2-digit in the string.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top