2

I have a 3 digit integer myInt:

int myInt = 809;

I need an output string *myStr which consists of the 2 last digits. Therefore

char *mystr = "09";

What would be the easiest solution to do that?

  • 4
    What have you tried? How did your attempt work or not work? Please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 07 '18 at 07:53
  • 1
    Apart from asking on SO and being told? [What have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/), and what problems did you run into? Show us your code for your best effort so far and we can help set you in the right direction. The interesting part is how you allocate the storage for the string containing the answer. There are many ways to do it, depending in part on how much you know of C and mostly depending on whether the code is in a function or in `main()` — another reason we need to see your best effort. Please read about how to create an MCVE ([MCVE]). – Jonathan Leffler Nov 07 '18 at 07:53
  • 2
    As a couple of hints: The modulo operator (`%`) and the [`snprintf`](https://en.cppreference.com/w/c/io/fprintf) function could be useful here. – Some programmer dude Nov 07 '18 at 07:54
  • `char *mystr` does *not* define a "string", but just a pointer to a `char`. Defining it does not allocate any space to store any `char`. – alk Nov 07 '18 at 08:15

5 Answers5

4

You can do like this:

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    char buf[32];
    int myInt = 809;
    char* mystr;
    snprintf(buf, sizeof buf, "%d", myInt);
    if (strlen(buf) > 2)
        mystr = buf + strlen(buf) - 2;
    else
        mystr = buf;
    fputs(mystr, stdout);
    return 0;
}
Yunbin Liu
  • 1,484
  • 2
  • 11
  • 20
  • Amazing! Thank you! – MasovyKnedlicek Nov 07 '18 at 08:01
  • 1
    `20` is a magic number, here. It is sufficient for the current architectures where `sizeof(int) <= 4`, but it may be wrong in a theoretical architecture where `sizeof(int )== 8`. It would be better to put the actual maximum length if an int, like the size needed to print `INT_MIN`. – Giovanni Cerretani Nov 07 '18 at 09:02
3

Here is a simple solution:

#include <stdio.h>

int main(void) {
    char buf[3];
    char *myStr;
    unsigned int myInt = 809;

    buf[0] = myInt / 10 % 10 + '0';  /* tens digit */
    buf[1] = myInt % 10 + '0';       /* unit digit */
    buf[2] = '\0';                   /* null terminator */
    myStr = buf;

    puts(mystr);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
2

No pointer magic necessary here. Just use plain math (the remainder operator: %), along with some suitable formatting:

#include <stdio.h>

int main(void)
{
  char a[3]; /* 2 characters for any value >= 0 and < 100. So we rely on the 100 below.
                +1 character for the `0`-terminator to make this a C-string*/
  int i = 809;

  assert(i >= 0); 

  sprintf(a, "%02d", i % 100);

  puts(a);
}

or avoiding magic numbers:

#include <stdio.h>

long long power_integer(int base, int x)
{
  if (0 == x)
  {
    return 1;
  }

  return base * power_integer(x - 1);
}

#define CUTOFF_BASE (10)
#define CUTOFF_DIGITS (2)

int main(void)
{
  char a[3]; 
  char f[8]; /* To hold "%01lld" to "%019lld". 
                (A 64 bit integer in decimal uses max 19 digits) */

  int i = 809;

  assert(i >= 0); 

  sprintf(f, "%%0%dlld*, CUTOFF_DIGITS);
  sprintf(a, f, i % power_integer(CUTOFF_BASE, CUTOFF_DIGITS));

  puts(a);
}

Output would be:

09
alk
  • 69,737
  • 10
  • 105
  • 255
2

For the specific case of turning "magic numbers" into strings, you can as well do that at compile-time. That is, when you have an integer constant rather than a run-time value:

#include <stdio.h>

#define VAL 809

#define STRINGIFY(x) #x
#define STR(i) STRINGIFY(i)
#define SUBSTR(i, n) &(STRINGIFY(i))[n]

int main (void)
{
  int whatever = VAL;

  puts(STR(VAL));
  puts(SUBSTR(VAL, 1));

  return 0;
}

Output:

809
09
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

You could try something like this:

#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
    int myInt = 809;
    char buf[16];  // buffer big enough to hold the string representation
    sprintf(buf, "%d", myInt); // write the string to the buffer
    char* myStr = &buf[strlen(buf) - 2]; // set a pointer to the second last position
    printf("myStr is %s\n", myStr);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Blaze
  • 16,736
  • 2
  • 25
  • 44