Member-only story
In C++, write a method to check if values of char[] arrays are complementary to each other
Question
Check if values are complementary to each other. For example, “1111” and “0000” are complementary to each other, while “1011” and “0101” are not complementary to each other.
Explanation
First check if the length of the 1st array is equal to the length of the 2nd array. If it’s not equal, then return false.
Next, create a for loop from i=0 to i < length of the first (or second array since it should be the same). Based on the same index, if the element of the first array is equal the the element of the second array, then return false. For example, if the first array has ‘0’, then the second array should be it’s complement which is ‘1’. If the program does not go into the if statement, then it will exit out of the for loop, and return true. True meaning that the 2 strings are complements to each other.
Solution
bool isComplement(char row1[],int len1,char row2[],int len2){if(len1 != len2){return false;
}for(int i=0 ; i < len1; i++){if( row1[i] == row2[i] ){return false;
}
}return true;}