C - reverse a number

后端 未结 6 1199
时光取名叫无心
时光取名叫无心 2020-12-18 05:58

I am coding in C on linux, and I need to reverse a number. (EG: 12345 would turn into 54321), I was going to just convert it into a string using itoa and then reverse that,

6条回答
  •  醉话见心
    2020-12-18 06:48

    If you really want to use strings, you can use sprintf to do what itoa does.

    int k = 12345;
    char str[40];
    sprintf(str,"%d",k);
    

    Then reverse the string and convert it back to int using atoi or sscanf.

提交回复
热议问题