Created
November 7, 2010 20:00
-
-
Save johnciacia/666373 to your computer and use it in GitHub Desktop.
Convert an integer to an ASCII string
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
/* K&R2 60-63 */ | |
char *itoa(int n, char s[]) | |
{ | |
int c, i, j, sign; | |
if((sign = n) < 0) | |
n = -n; | |
do { | |
s[i++] = n % 10 + '0'; | |
} while((n /= 10) > 0); | |
if(sign < 0) | |
s[i++] = '-'; | |
s[i] = '\0'; | |
for (i = 0, j = strlen(s)-1; i < j; i++, j--) { | |
c = s[i]; | |
s[i] = s[j]; | |
s[j] = c; | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment