-1

How do I reverse decimal number. I wrote this code but reverses the integer.

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <math.h>
long reverse(long);

int main()
{
   long n, r;

   scanf("%ld", &n);

   r = reverse(n);

   printf("%ld\n", r);
   getch();
   return 0;
}

long reverse(long n)
{
   static long r = 0;

   if (n == 0) 
      return 0;

   r = r * 10;
   r = r + n % 10;
   reverse(n/10);
   return r;
}

I can not separate the integer of the decimal.
How should I do it.

Axalo
  • 2,953
  • 4
  • 25
  • 39
ali
  • 9
  • 2

1 Answers1

0

Use the reverse_string function submitted by @GManNickG:

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

void reverse_string(char *str);

int main(){   
  char number_to_reverse[256];

  scanf("%s",number_to_reverse);

  reverse_string(number_to_reverse);

  printf("%s",number_to_reverse);

  return 0; 
}

 void reverse_string(char *str) {
    /* skip null */
    if (str == 0)
    {
      return;
    }

    /* skip empty string */
    if (*str == 0)
    {
      return;
    }

    /* get range */
    char *start = str;
    char *end = start + strlen(str) - 1; /* -1 for \0 */
    char temp;

    /* reverse */
    while (end > start)
    {
      /* swap */
      temp = *start;
      *start = *end;
      *end = temp;

      /* move */
      ++start;
      --end;
    }
 }
Community
  • 1
  • 1
user1717828
  • 7,122
  • 8
  • 34
  • 59