Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to extract element from a string

Status
Not open for further replies.

chintimz2

Programmer
Oct 15, 2005
1
AU
This function involves extracting the day, month, year from a processed string.

my code:

int main(void)
{
int date = atoi("161005");
int day = date/10000;
int month = (date/100) % 100;
int year = (date % 100);
printf("%02d/%02d/%02d",day,month,year);
return 0;
}

it works perfectly when the values are in the form dd:mm:yy
however the breaks down when you give it inputs such as 1/1/5 or 1/11/5 or 11/1/5

for 1/1/5 it return 00/1/05
for 1/11/5 it return 01/11/05
for 11/1/5 it return 00/11/15

I was just wondering is there a faster and more efficent way of designing it, instead of using switch statements.
Thanks :D
 
> for 1/11/5 it return 01/11/05
> for 11/1/5 it return 00/11/15
If you just type in 1115, then there is no way you can figure out which of those two valid dates should be used.

Either
- insist on 6-digit input strings, and reject any which fail this basic test.
- insist on using separators like / or : to make it clear where the boundaries are.





--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top