0

Task is to convert int to char . Here is my code for converting

#include <conio.h>
#include <stdio.h>

main(void)
{
int number,reserve ;
scanf_s("%d",&number);
if (number > 0 || number == 0)
{
    do
    {
        reserve = number % 10;
        printf("%c", reserve + '/0');
        number /= 10;
    } while (number != 0);
}
else
{
    number *= -1;
    printf("-");
    do
    {
        reserve = number % 10;
        printf("%c", reserve + '/0');
        number /= 10;
    } while (number != 0);

}
_getch();
return 0;
}

The problem is this is printing the result in the reversed of what I want. So I'm searching for a way to reverse it back. However my code is printing char by char. Probably I just need to save all chars into some string but I`m not sure of how do it. Appreciate any help.

hbagdi
  • 485
  • 6
  • 18
andrewand
  • 23
  • 4
  • So you want to store those characters and print them in reverse order? – hbagdi Dec 02 '16 at 03:55
  • Yes you got me right – andrewand Dec 02 '16 at 03:56
  • Have you tried storing it in an array and then finally printing the array in the reverse order? Or a stack? – hbagdi Dec 02 '16 at 03:57
  • i thought about array but i don't know how write it properly – andrewand Dec 02 '16 at 04:00
  • Note: `number *= -1;` is UB if `number == INT_MIN` with 2's complement. – chux - Reinstate Monica Dec 02 '16 at 04:04
  • @chux: Probably yes, but do you really expect andrewand to understand that problem now? You could explain that to him in two years, but today, it's not the right time to tell him this. – Bodo Thiesen Dec 02 '16 at 04:05
  • 1
    @BodoThiesen Comments are a good place for discussion abouts issues about the post like [relevant but minor or transient information](http://stackoverflow.com/help/privileges/comment) and comments/answers. Comments about [OP and SO users](http://stackoverflow.com/questions/40924503/converting-int-to-char-and-reversing#comment69060791_40924503) are better handled in chat or meta. – chux - Reinstate Monica Dec 02 '16 at 04:17
  • `number *= -1;` => `number = -number;` – phuclv Dec 02 '16 at 15:30

3 Answers3

1

Have you tried storing them in an array and printing the array in reverse?

#include <stdio.h>

int main(void)
{
    int number,reverse,i ;
    scanf("%d",&number);
    char string[20];
    int index = 0;
    if (number< 0){
       number *= -1;
       printf("-");
    }
    do
    {
        reverse = number % 10;
        //printf("%c", reverse);
        string[index++] = reverse;
        number /= 10;
    } while (number != 0);

    for (i = index ; i >= 0 ; i--)
        printf("%c", string[i]);
    return 0;
}
hbagdi
  • 485
  • 6
  • 18
0

As you assumed in your question, you need to store the characters to print them reverse.

Using an array for this task is quite simple:

// I never know how much memory I need ...
char reverse[200];
int i = 0;
reverse[i++] = next calculated character

later

while (i) {
    print reverse[--i];
}
Bodo Thiesen
  • 2,476
  • 18
  • 32
0

This method should suffice in converting an integer to a string without using any standard library functions.

void ReverseString(char *sBuf, int iLength)
{
    char *String1 = NULL;
    char *String2 = NULL;

    for (String1 = sBuf, String2 = sBuf + iLength - 1; String2 > String1; ++String1, --String2)
    {
        *String1 ^= *String2;
        *String2 ^= *String1;
        *String1 ^= *String2;
    }
}

int YourVeryOwn_itoa(int iINValue, char *sOUTValue)
{
    unsigned short writePosition = 0;

    if (iINValue < 0)
    {
        sOUTValue[writePosition++] = '-';
    }

    do
    {
        sOUTValue[writePosition++] = (unsigned char)48 + (iINValue % 10);
    } while ((iINValue /= 10) > 0);

    sOUTValue[writePosition] = '\0';

    ReverseString(sOUTValue, writePosition);

    return writePosition;
}

void main()
{
    char buf[255];
    YourVeryOwn_itoa(123456789, buf);
    printf("%s", buf);
}
NTDLS
  • 4,757
  • 4
  • 44
  • 70
  • are you trying to do [xor swap](https://en.wikipedia.org/wiki/XOR_swap_algorithm)? it's a very bad idea. [`void main()`](http://stackoverflow.com/q/204476/995714) is also a bad idea – phuclv Dec 02 '16 at 15:29
  • The previous rendition I did here actually mentioned that XOR was a bad idea for all of the known reasons (I like it because its quick to type). Also, as an example void main() is perfectly acceptable. – NTDLS Dec 03 '16 at 01:38