Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 6th, 2003, 01:09 PM
Junior Member

Join Date: May 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
returning an appropriate string for an expected int value??

Hi can anyone help?

Im trying to write a single function which sets a small array with 2 characters to the ordinal suffix of a number. 1st, 2nd, 3rd 4th etc

and the suffix refers to the laters.

my function prototype is

void suffix(int n, char suff[]);

and i have made the following declarations

char s[5];
int x;

and with the result of the call

suffix(x,s);

will give the appropriate string in s for any given positive value of x from 0 to 9999

a possible pattern i have found is that 4 - 20 end in th

then the first three after that have st,nd,rd

is there a command that can read the last digit of an integer number ?

because if there is then i can use conditions easier and compare the last digit against the tests to see what its suffix ( letters ) are.

any help much appreciated.

#2
March 6th, 2003, 01:31 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 40 m 8 sec
Reputation Power: 4106
Find the result of the number modulo 10. That will give you the last digit.

last_digit = number % 10;

#3
March 6th, 2003, 02:40 PM
Junior Member

Join Date: May 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
im not sure if that will work

if the user types 5006 the suffix will be 5006 th

because the last digit is 6 which has a suffix of th

i will get a decimal number when im storing in an integer.

#4
March 6th, 2003, 03:00 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 40 m 8 sec
Reputation Power: 4106
>> i will get a decimal number when im storing in an integer.
I'm not sure what you mean by this. Do you mean a floating point number?? You're not going to get floating point numbers when your variables are declared as int.
Code:
```void suffix(int n, char suff[]) {
int last_digit = n % 10;

if (last_digit == 1)
sprintf(suff, "%dst", n);
else if (last_digit == 2)
sprintf(suff, "%dnd", n);
else if (last_digit == 3)
sprintf(suff, "%drd", n);
else
sprintf(suff, "%dth", n);
}```

Hope this helps!

#5
March 6th, 2003, 04:11 PM
Junior Member

Join Date: May 2002
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
 Originally posted by Scorpions4ever >> i will get a decimal number when im storing in an integer. I'm not sure what you mean by this. Do you mean a floating point number?? You're not going to get floating point numbers when your variables are declared as int. Code: ```void suffix(int n, char suff[]) { int last_digit = n % 10; if (last_digit == 1) sprintf(suff, "%dst", n); else if (last_digit == 2) sprintf(suff, "%dnd", n); else if (last_digit == 3) sprintf(suff, "%drd", n); else sprintf(suff, "%dth", n); }``` Hope this helps!

could you do me a favour and explain why you are using modulus 10 and what the sprintf command is doing. I havent come across it before thats why. Will your method work if the user types in 6777 as the number will it be able to recongnize the last digit which is 7 and assign it a th ?

#6
March 6th, 2003, 05:26 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 40 m 8 sec
Reputation Power: 4106
>> could you do me a favour and explain why you are using modulus 10
It's modulo, not modulus. What the modulo operator does is to return the remainder of a division operation. For example, 5437 modulo 10 returns 7 (i.e. 5437 / 10 = 543 with a remainder of 7). Hence, when you modulo divide a number by 10, you get the last digit.

>> what the sprintf command is doing
sprintf works similar to printf, except it puts the result into the first argument, instead of the screen. I'm assuming here that you know what the printf() function does. So in our case sprintf(suff, "%dst", n) will put the result into the suff char array.

>> Will your method work if the user types in 6777
Yes it will.

#7
March 6th, 2003, 10:02 PM
 Jason Doucette
jasondoucette.com

Join Date: Feb 2003
Posts: 378
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Scorpions4ever, remember what Neildadon stated in the first post: the numbers 11, 12 and 13 do not end with 'st', 'nd' or 'rd' - they end with 'th'. Your code does not check these conditions. The last 2 digits of the number must be checked for these 3 cases (x % 100 would suffice to get the last 2 digits). Then, your code would be fine (x %10 to get the last digit).

#8
March 6th, 2003, 11:29 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 40 m 8 sec
Reputation Power: 4106
Good call. I was only paying attention to this part of his post:
>> is there a command that can read the last digit of an integer number ?
That's the part I was paying attention to

#9
March 7th, 2003, 07:34 AM
 Jason Doucette
jasondoucette.com

Join Date: Feb 2003
Posts: 378
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Understood. I just thought I'd mention it in case a passerby grabbed your code unknowingly thinking it would handle the original question asked.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > returning an appropriate string for an expected int value??