The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
returning an appropriate string for an expected int value??
Discuss returning an appropriate string for an expected int value?? in the C Programming forum on Dev Shed. returning an appropriate string for an expected int value?? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 6th, 2003, 12: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.
|

March 6th, 2003, 12:31 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Find the result of the number modulo 10. That will give you the last digit.
last_digit = number % 10;
|

March 6th, 2003, 01: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.
|

March 6th, 2003, 02:00 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
>> 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! 
|

March 6th, 2003, 03: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! |
thanks for the reply
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 ?
|

March 6th, 2003, 04:26 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
>> 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.
|

March 6th, 2003, 09:02 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
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).
|

March 6th, 2003, 10:29 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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 
|

March 7th, 2003, 06:34 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|