Browse Source

refactoring: used length for indexing instead of index

remotes/origin/conversionOfNumbers
Laurin 11 months ago
parent
commit
501ebf2245
  1. 9
      src/conversionOfNumbers/conversionOfNumbers.c

9
src/conversionOfNumbers/conversionOfNumbers.c

@ -6,14 +6,13 @@ char* convertIntToBinaryStr(int input){
int length = 2; int length = 2;
int rest; int rest;
char* result = (char*)malloc(sizeof(char) * length + 1); char* result = (char*)malloc(sizeof(char) * length + 1);
int index = length - 1;
result[length] = '\0';
do{ do{
rest = input % 2; rest = input % 2;
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); }while(input != 0);
result[length] = '\0';
return result; return result;
} }
Loading…
Cancel
Save