diff --git a/C/KaratsubaAlgorithm.c b/C/KaratsubaAlgorithm.c new file mode 100644 index 0000000..bdf7303 --- /dev/null +++ b/C/KaratsubaAlgorithm.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int KaratsubaAlgorithm(char* str1, char* str2){ + int i = 0, j = 0; + int num1 = 0, num2 = 0; + while (str1[i] != '\0') + { + num1 *= 2; + if (str1[i] == '0' || str1[i] == '1') num1 += str1[i] - '0'; + else return -1; + i++; + } + while (str2[j] != '\0') + { + num2 *= 2; + if (str2[j] == '0' || str2[j] == '1') num2 += str2[j] - '0'; + else return -1; + j++; + } + return num1 * num2; +} \ No newline at end of file diff --git a/C/README.md b/C/README.md index 8b13789..131dde6 100644 --- a/C/README.md +++ b/C/README.md @@ -1 +1,2 @@ - +# KaratsubaAlgorithm +Karatsuba Algorithm takes two strings are function arguments. Converts them from binary string to a decimal value, and return product of the two numbers. If the numbers are invalid, or any character in the string is neither 1 nor 0, -1 is returned.