For an assignment, we have to write a program that takes a Hamming code as input and outputs if there is an error, and then corrects the error. Once I compile the program, I keep getting either a segmentation fault or it just gets stuck in a loop and does not do anything. The code I have so far:

#include<string.h> #include<math.h> #include<stdlib.h> #include<stdio.h> void check_hamming(char *hamming_string, int parity){ int length, num_parity, i, j, start, k; printf("Enter the Hamming code: "); scanf("%s",hamming_string); int error_bit = 0; length = strlen(hamming_string); num_parity = ceil(log(length)/log(2)); for(i = 0; i < num_parity; i++){ start = pow(2, i); int check_parity = parity; for(j = start; j < length; j=(2*start)){ for(k = j; (k < ((2*j) - 1)) && (k < length); k++){ check_parity ^= (hamming_string[length - k] - '0'); }//end for k }//end for j error_bit = error_bit + (check_parity*start); }//end for i if(error_bit == 0) printf("No error \n"); else{ printf("There is an error in bit: %d", error_bit); if(hamming_string[length - error_bit] == '0') hamming_string[length - error_bit] = '1'; else hamming_string[length - error_bit] = '0'; printf("The corrected Hamming code is: %s \n", hamming_string); }//end else }//end check_hamming

I also have a main, as well and a function to enter the parameters, but I don't think those are the problems since everything works fine until I enter the hamming code. Let me know if it would help if I posted them. Any help would be greatly appreciated!

Thanks!