From 501ebf2245962ff7b219cda5d1c271c0bdd6153e Mon Sep 17 00:00:00 2001 From: Laurin Date: Tue, 30 Jan 2024 23:26:25 +0100 Subject: [PATCH] refactoring: used length for indexing instead of index --- src/conversionOfNumbers/conversionOfNumbers.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/conversionOfNumbers/conversionOfNumbers.c b/src/conversionOfNumbers/conversionOfNumbers.c index 64dae01..b7ae51b 100644 --- a/src/conversionOfNumbers/conversionOfNumbers.c +++ b/src/conversionOfNumbers/conversionOfNumbers.c @@ -6,14 +6,13 @@ char* convertIntToBinaryStr(int input){ int length = 2; int rest; char* result = (char*)malloc(sizeof(char) * length + 1); - int index = length - 1; + result[length] = '\0'; do{ rest = input % 2; input /= 2; - if(rest == 1) result[index] = '1'; - if(rest == 0) result[index] = '0'; - index--; + if(rest == 1) result[length - 1] = '1'; + if(rest == 0) result[length - 1] = '0'; + length--; }while(input != 0); - result[length] = '\0'; return result; } \ No newline at end of file