Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- int strToInt(char *s)
- {
- int tmp = 0, minus = 0;
- if (*s == '-') {
- minus = 1;
- *s++;
- }
- while(*s)
- tmp = 10 * tmp + *s++ - 48;
- return minus ? -tmp : tmp;
- }
- char *intToStr(int n)
- {
- int i = 0, g = 0, minus = 0;
- char *tmp = (char*)malloc(sizeof(char)), *rev = (char*)malloc(sizeof(char));
- if (n < 0) {
- minus = 1;
- n = abs(n);
- }
- do {
- tmp[i] = n % 10 + 48;
- n -= n % 10;
- i++;
- }while(n /= 10);
- if(minus) {
- tmp[i++] = '-';
- }
- for(i--; i >= 0; i--) {
- rev[i] = tmp[g++];
- }
- return rev;
- }
- int main () {
- printf("%d", strToInt(intToStr(-15)));
- getchar();
- return 0;
- }
Add Comment
Please, Sign In to add comment