-1

How would one concatenate integers. Like, let's just say I made a simple program like this:

int concatenate(int i, int j) {

}

And I gave that function two integers. i would be 1 and j would be 9. How would I be able to concatenate numbers so that I could get 123456789?

Jerry K
  • 31
  • 1
  • 1
  • I was thinking about having an int called `count` and then as count increments, it also adds that number onto the previous number. So if `i` was 3 and `j` was 5, `count` would also be equal to `i` and adds a number until it hits `j`. – Jerry K May 31 '16 at 02:59
  • What is the expected output if `i` is 3 and `j` is `5`? – Pang May 31 '16 at 03:00
  • @Pang, `345` Just trying something silly that I thought of. :P – Jerry K May 31 '16 at 03:03
  • possible duplication http://stackoverflow.com/questions/12700497/how-to-concatenate-two-integers-in-c – Nirupa May 31 '16 at 03:05
  • What will happen if `i > j`? Please add more details regarding your question. – CroCo May 31 '16 at 03:17
  • And what if `i` or `j` is greater than 10 or if they are negative? You should "[edit]" your question to provide more details. – Pang May 31 '16 at 03:25

3 Answers3

1

try this:

int concatenate(int i, int j) {
    int result = 0;
    for (int x = i; x <= j; x++) {
       result = result * 10 + x; 
    }
    return result;
}
Alastair Brown
  • 1,598
  • 7
  • 12
0

Another way using std::to_string

#include <string>

int concatenate(int i, int j) {
    std::string result = "";
    for (; i <= j; ++i)
        result += std::to_string(i);
    return std::stoi(result);
}

But it may easily overflow. Try changing return type to long or higher. Then, std::stol would be useful.

Shreevardhan
  • 11,453
  • 3
  • 36
  • 47
0

If you restrict i and j to the domain [0,9], then a 10x10 lookup table would be a performant solution:

long concatenate_lookup[10][10] = {
    {         0L,         1L,       12L,     123L,   1234L, 12345L, 123456L, 1234567L, 12345678L, 123456789L},
    {        10L,         1L,       12L,     123L,   1234L, 12345L, 123456L, 1234567L, 12345678L, 123456789L},
    {       210L,        21L,        2L,      23L,    234L,  2345L,  23456L,  234567L,  2345678L,  23456789L},
    {      3210L,       321L,       32L,       3L,     34L,   345L,   3456L,   34567L,   345678L,   3456789L},
    {     43210L,      4321L,      432L,      43L,      4L,    45L,    456L,    4567L,    45678L,    456789L},
    {    543210L,     54321L,     5432L,     543L,     54L,     5L,     56L,     567L,     5678L,     56789L},
    {   6543210L,    654321L,    65432L,    6543L,    654L,    65L,      6L,      67L,      678L,      6789L},
    {  76543210L,   7654321L,   765432L,   76543L,   7654L,   765L,     76L,       7L,       78L,       789L},
    { 876543210L,  87654321L,  8765432L,  876543L,  87654L,  8765L,    876L,      87L,        8L,        89L},
    {9876543210L, 987654321L, 98765432L, 9876543L, 987654L, 98765L,   9876L,     987L,       98L,         9L}
};

long concatenate(long i, long j ) {
    assert(i>=0 && i<=9 && j>=0 && j<=9);
    return concatenate_lookup[i][j];
} // end concatenate()

It is necessary to use long instead of int to handle the full range of possible outputs.

bgoldst
  • 32,336
  • 5
  • 36
  • 61