Created
March 13, 2017 19:33
-
-
Save aaronryank/808d667c472af123e6ca08d0aacfcebc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* apparently I can't write a freaking string-reverse algorithm | |
stackoverflow.com/a/784567/6850771 */ | |
char *reverse(char *str) | |
{ | |
char tmp, *src, *dst; | |
size_t len; | |
if (str != NULL) | |
{ | |
len = strlen (str); | |
if (len > 1) { | |
src = str; | |
dst = src + len - 1; | |
while (src < dst) { | |
tmp = *src; | |
*src++ = *dst; | |
*dst-- = tmp; | |
} | |
} | |
} | |
return str; | |
} | |
/* itoa from K&R */ | |
void itoa(int n, char s[]) | |
{ | |
int i, sign; | |
if ((sign = n) < 0) /* record sign */ | |
n = -n; /* make n positive */ | |
i = 0; | |
do { /* generate digits in reverse order */ | |
s[i++] = n % 10 + '0'; /* get next digit */ | |
} while ((n /= 10) > 0); /* delete it */ | |
if (sign < 0) | |
s[i++] = '-'; | |
reverse(s); | |
s[i] = '\0'; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For http://stackoverflow.com/a/42771998/6850771