Created
August 9, 2024 14:50
-
-
Save giorgi568/0de6f26662b69b03783522e8609823f8 to your computer and use it in GitHub Desktop.
the c programming language book, exercise 5-6
This file contains hidden or 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
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <stdio.h> | |
| #include <limits.h> | |
| /* | |
| code is from the book, i only added minimal witdth -- w | |
| */ | |
| void itoa(int n, char s[], int w); | |
| void reverse(char s[]); | |
| int main(void) { | |
| char buffer[20]; | |
| printf("INT_MIN: %d\n", INT_MIN); | |
| itoa(INT_MIN, buffer, 10); | |
| printf("Buffer : %s\n", buffer); | |
| return 0; | |
| } | |
| void itoa(int n, char s[], int w) { | |
| int i, sign; | |
| sign = n; | |
| i = 0; | |
| do { | |
| s[i++] = abs(n % 10) + '0'; | |
| } while ( n /= 10 ); | |
| if (sign < 0) | |
| s[i++] = '-'; | |
| while(i < w) { | |
| s[i++] = ' '; | |
| } | |
| s[i] = '\0'; | |
| reverse(s); | |
| } | |
| void reverse(char s[]) { | |
| int c, i, j; | |
| for ( i = 0, j = strlen(s)-1; i < j; i++, j--) { | |
| c = s[i]; | |
| s[i] = s[j]; | |
| s[j] = c; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment