• Status: Solved
• Priority: Medium
• Security: Public
• Views: 240

# how to determine a number ending with 12

Hi experts,

I have 2 columns as following:

char row_elem[50][50];

row_elem[6]                          row_elem[9]

17896412                              1/12/2004
18889712                              10/12/2004
19996512                              12/5/2004
98765409                              1/5/2004

I want to check row_elem[6], if ends with 12, then add 1 day in row_elem[9]. So the results should be

17896412                              1/13/2004
18889712                              10/13/2004
19996512                              12/6/2004
98765409                              1/5/2004

How can I do that ? Thanks
0
justinY
• 2
• 2
• 2
• +1
1 Solution

Commented:
Hi justinY,
if(!memcmp("12",&row_elem[6],2)){
...
}

Cheers!

Stefan
0

Commented:
You can check for a number ending in 12 by dividing by 100 and taking the remainder:

if(row_elem[6]%100==12)

0

Author Commented:
Thanks, then how do I add one to the day ?
0

Commented:
Sorry, your row_elem are character; I missed that. A comparison would be better in that case.

The memcmp that Stefan relevantly proposes is a good test. It requires that the numbers all be the same length.
0

Author Commented:
So, if I have

17896412                          1/13/2004
78912                               2/13/2004

then I cannot use memcmp(), right ?
0

Commented:
If the strings vary in length you can do:

if (!memcmp("12", &row_elem[6][strlen(row_elem[6])-2], 2)) {

}

or

if (atoi(row_elem[6])%100 == 12) {
}

0

Commented:
> how do I add one to the day ?

Use a time_t variable (which is the number of seconds since 01/01/1970), and add 60*60*24 = 86400. You can then convert it back to a struct tm* by localtime().

That eliminates lengthy calendar operations, like checking for end-of-month, leap years and such.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 2
• 2
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.