473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Remove first two characters from char* or from int?

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.

Mar 6 '08 #1
3 4106
In article <58fb4e42-cbe5-48ad-bea6-4149eb5237a1
@p25g2000hsf.googlegroups.com>, yo**********@yahoo.com says...
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);

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 6 '08 #2
On Mar 5, 10:01*pm, Jerry Coffin <jcof...@taeus.comwrote:
In article <58fb4e42-cbe5-48ad-bea6-4149eb5237a1
@p25g2000hsf.googlegroups.com>, yogi_bear...@yahoo.com says...
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);

--
* * Later,
* * Jerry.

The universe is a figment of its own imagination.
Perfect! Thanks!
Mar 6 '08 #3
yogi_bear_79 wrote:
>c = atoi(year+2);
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.)
Mar 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Toto | last post by:
Hello, my problem is quite simple to explain. I have the following string: "table+camera" and I want to remove the + sign: "tablecamera". How do i do that ?
2
by: collinm | last post by:
hi here my code FILE *fp; char *line; #define LINE_MAX 30 fp = fopen("test1.txt", "r");
4
by: francescomoi | last post by:
Hi. I'm trying to remove some characters within a string and substitute others. For instance, I want to convert: John's new house, great ---> Johns-new-house-great I tried with:...
11
by: deko | last post by:
I need to loop through a string and remove all characters except numbers or letters. I am getting an ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the...
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: cody | last post by:
Is there a method to replace special characters like Ä (A-Umlaut) with A, Ö (O-Umlaut) with O, and so on? Sure, I could look for each character separately and replace it with its...
5
by: Andrew Wingorodov | last post by:
im made subj like this: inline bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; } str.erase ( std.begin () , std::find_if ( str.begin(), str.end(), isnt_space) );
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.