From 1d8d07efc9c0f3bdecc4faef991f0a1ac77b2146 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 8 Apr 2020 09:45:12 -0400 Subject: [PATCH] gets not a C11 standard, superceded by fgets --- conversions/hexadecimal_to_octal.c | 175 +++++++++++++++-------------- 1 file changed, 88 insertions(+), 87 deletions(-) diff --git a/conversions/hexadecimal_to_octal.c b/conversions/hexadecimal_to_octal.c index 097ba87c..7340dcd1 100644 --- a/conversions/hexadecimal_to_octal.c +++ b/conversions/hexadecimal_to_octal.c @@ -4,81 +4,82 @@ int main() { - char hex[17]; +#define MAX_STR_LEN 17 + char hex[MAX_STR_LEN]; long long octal, bin, place; int i = 0, rem, val; /* Input hexadecimal number from user */ printf("Enter any hexadecimal number: "); - gets(hex); + fgets(hex, MAX_STR_LEN, stdin); octal = 0ll; bin = 0ll; place = 0ll; /* Hexadecimal to binary conversion */ - for(i=0; hex[i]!='\0'; i++) + for (i = 0; hex[i] != '\0'; i++) { bin = bin * place; - switch(hex[i]) + switch (hex[i]) { - case '0': - bin += 0; - break; - case '1': - bin += 1; - break; - case '2': - bin += 10; - break; - case '3': - bin += 11; - break; - case '4': - bin += 100; - break; - case '5': - bin += 101; - break; - case '6': - bin += 110; - break; - case '7': - bin += 111; - break; - case '8': - bin += 1000; - break; - case '9': - bin += 1001; - break; - case 'a': - case 'A': - bin += 1010; - break; - case 'b': - case 'B': - bin += 1011; - break; - case 'c': - case 'C': - bin += 1100; - break; - case 'd': - case 'D': - bin += 1101; - break; - case 'e': - case 'E': - bin += 1110; - break; - case 'f': - case 'F': - bin += 1111; - break; - default: - printf("Invalid hexadecimal input."); + case '0': + bin += 0; + break; + case '1': + bin += 1; + break; + case '2': + bin += 10; + break; + case '3': + bin += 11; + break; + case '4': + bin += 100; + break; + case '5': + bin += 101; + break; + case '6': + bin += 110; + break; + case '7': + bin += 111; + break; + case '8': + bin += 1000; + break; + case '9': + bin += 1001; + break; + case 'a': + case 'A': + bin += 1010; + break; + case 'b': + case 'B': + bin += 1011; + break; + case 'c': + case 'C': + bin += 1100; + break; + case 'd': + case 'D': + bin += 1101; + break; + case 'e': + case 'E': + bin += 1110; + break; + case 'f': + case 'F': + bin += 1111; + break; + default: + printf("Invalid hexadecimal input."); } place = 10000; @@ -87,36 +88,36 @@ int main() place = 1; /* Binary to octal conversion */ - while(bin > 0) + while (bin > 0) { rem = bin % 1000; - switch(rem) + switch (rem) { - case 0: - val = 0; - break; - case 1: - val = 1; - break; - case 10: - val = 2; - break; - case 11: - val = 3; - break; - case 100: - val = 4; - break; - case 101: - val = 5; - break; - case 110: - val = 6; - break; - case 111: - val = 7; - break; + case 0: + val = 0; + break; + case 1: + val = 1; + break; + case 10: + val = 2; + break; + case 11: + val = 3; + break; + case 100: + val = 4; + break; + case 101: + val = 5; + break; + case 110: + val = 6; + break; + case 111: + val = 7; + break; } octal = (val * place) + octal; @@ -129,4 +130,4 @@ int main() printf("Octal number = %lld", octal); return 0; -} +}