Guest User

Untitled

a guest
May 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. int strToInt(char *s)
  5. {
  6.     int tmp = 0, minus = 0;
  7.    
  8.     if (*s == '-') {
  9.         minus = 1;
  10.         *s++;
  11.     }
  12.    
  13.     while(*s)
  14.         tmp = 10 * tmp + *s++ - 48;
  15.    
  16.     return minus ? -tmp : tmp;
  17. }
  18.  
  19.  
  20. char *intToStr(int n)
  21. {
  22.     int i = 0, g = 0, minus = 0;
  23.     char *tmp = (char*)malloc(sizeof(char)), *rev = (char*)malloc(sizeof(char));
  24.  
  25.     if (n < 0) {
  26.         minus = 1;
  27.         n = abs(n);
  28.     }
  29.    
  30.     do {
  31.         tmp[i] = n % 10 + 48;
  32.         n -= n % 10;
  33.        
  34.         i++;
  35.     }while(n /= 10);
  36.    
  37.     if(minus) {
  38.         tmp[i++] = '-';
  39.     }
  40.    
  41.     for(i--; i >= 0; i--) {
  42.         rev[i] = tmp[g++];
  43.     }
  44.    
  45.     return rev;
  46. }
  47.  
  48. int main () {
  49.    
  50.     printf("%d", strToInt(intToStr(-15)));
  51.    
  52.     getchar();
  53.     return 0;
  54. }
Add Comment
Please, Sign In to add comment