8
8
* pair of corresponding bits and any carry from the previous addition, it
9
9
* calculates the sum. If the sum exceeds 1, a carry is generated for the next
10
10
* bit. The results for each bit are collected in a result string, which is
11
- * reversed at the end to present the final binary sum correctly. This method
12
- * efficiently handles varying string lengths and carrying during addition.
11
+ * reversed at the end to present the final binary sum correctly. Additionally,
12
+ * the function validates the input to ensure that only valid binary strings
13
+ * (containing only '0' and '1') are processed. If invalid input is detected,
14
+ * it returns an empty string.
13
15
* @author [Muhammad Junaid Khalid](https://github.com/mjk22071998)
14
16
*/
15
17
16
18
#include < algorithm> // / for reverse function
17
19
#include < cassert> // / for tests
18
20
#include < iostream> // / for input and outputs
19
- #include < string> // / for sting class
21
+ #include < string> // / for string class
20
22
21
23
/* *
22
24
* @namespace
@@ -33,9 +35,14 @@ class BinaryAddition {
33
35
*
34
36
* @param a The first binary string.
35
37
* @param b The second binary string.
36
- * @return The sum of the two binary strings as a binary string.
38
+ * @return The sum of the two binary strings as a binary string, or an empty string
39
+ * if either input string contains non-binary characters.
37
40
*/
38
41
std::string addBinary (const std::string& a, const std::string& b) {
42
+ if (!isValidBinaryString (a) || !isValidBinaryString (b)) {
43
+ return " " ; // Return empty string if input contains non-binary characters
44
+ }
45
+
39
46
std::string result;
40
47
int carry = 0 ;
41
48
int maxLength = std::max (a.size (), b.size ());
@@ -58,15 +65,28 @@ class BinaryAddition {
58
65
std::reverse (result.begin (), result.end ());
59
66
return result;
60
67
}
68
+
69
+ private:
70
+ /* *
71
+ * Validates whether a string contains only binary characters (0 or 1).
72
+ * @param str The string to validate.
73
+ * @return true if the string is binary, false otherwise.
74
+ */
75
+ bool isValidBinaryString (const std::string& str) const {
76
+ return std::all_of (str.begin (), str.end (), [](char c) {
77
+ return c == ' 0' || c == ' 1' ;
78
+ });
79
+ }
61
80
};
62
81
} // namespace greedy_algorithms
63
82
64
83
/* *
65
84
* Function to run tests for the addBinary method.
66
85
*/
67
86
void tests () {
68
- BinaryAddition binaryAddition;
87
+ greedy_algorithms:: BinaryAddition binaryAddition;
69
88
89
+ // Valid binary string tests
70
90
assert (binaryAddition.addBinary (" 1010" , " 1101" ) == " 10111" );
71
91
assert (binaryAddition.addBinary (" 1111" , " 1111" ) == " 11110" );
72
92
assert (binaryAddition.addBinary (" 101" , " 11" ) == " 1000" );
@@ -78,14 +98,22 @@ void tests() {
78
98
" 110110110110110110110110110110" ) ==
79
99
" 1100001100001100001100001100000" );
80
100
assert (binaryAddition.addBinary (" 1" , " 11111111" ) == " 100000000" );
81
- assert (binaryAddition.addBinary (" " , " " ) == " " );
82
101
assert (binaryAddition.addBinary (" 10101010" , " 01010101" ) == " 11111111" );
102
+
103
+ // Invalid binary string tests (should return empty string)
104
+ assert (binaryAddition.addBinary (" 10102" , " 1101" ) == " " );
105
+ assert (binaryAddition.addBinary (" ABC" , " 1101" ) == " " );
106
+ assert (binaryAddition.addBinary (" 1010" , " 1102" ) == " " );
107
+ assert (binaryAddition.addBinary (" 111" , " 1x1" ) == " " );
108
+ assert (binaryAddition.addBinary (" 1x1" , " 111" ) == " " );
109
+ assert (binaryAddition.addBinary (" 1234" , " 1101" ) == " " );
83
110
}
84
111
85
112
/* *
86
113
* Main function to execute the program.
87
114
*/
88
115
int main () {
89
116
tests ();
117
+ std::cout << " All tests passed.\n " ;
90
118
return 0 ;
91
119
}
0 commit comments