#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    12
    Rep 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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Find the result of the number modulo 10. That will give you the last digit.

    last_digit = number % 10;
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    12
    Rep 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.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    >> 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! :)
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    12
    Rep Power
    0
    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 ?
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    >> 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.
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    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).
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    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 :)
  16. #9
  17. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Understood. ;) I just thought I'd mention it in case a passerby grabbed your code unknowingly thinking it would handle the original question asked.

IMN logo majestic logo threadwatch logo seochat tools logo