Skip to content

Commit e62242e

Browse files
committed
fix: Fixed the case of non binary strings
1 parent d9113db commit e62242e

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

greedy_algorithms/binary_addition.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
* pair of corresponding bits and any carry from the previous addition, it
99
* calculates the sum. If the sum exceeds 1, a carry is generated for the next
1010
* 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.
1315
* @author [Muhammad Junaid Khalid](https://github.com/mjk22071998)
1416
*/
1517

1618
#include <algorithm> /// for reverse function
1719
#include <cassert> /// for tests
1820
#include <iostream> /// for input and outputs
19-
#include <string> /// for sting class
21+
#include <string> /// for string class
2022

2123
/**
2224
* @namespace
@@ -33,9 +35,14 @@ class BinaryAddition {
3335
*
3436
* @param a The first binary string.
3537
* @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.
3740
*/
3841
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+
3946
std::string result;
4047
int carry = 0;
4148
int maxLength = std::max(a.size(), b.size());
@@ -58,15 +65,28 @@ class BinaryAddition {
5865
std::reverse(result.begin(), result.end());
5966
return result;
6067
}
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+
}
6180
};
6281
} // namespace greedy_algorithms
6382

6483
/**
6584
* Function to run tests for the addBinary method.
6685
*/
6786
void tests() {
68-
BinaryAddition binaryAddition;
87+
greedy_algorithms::BinaryAddition binaryAddition;
6988

89+
// Valid binary string tests
7090
assert(binaryAddition.addBinary("1010", "1101") == "10111");
7191
assert(binaryAddition.addBinary("1111", "1111") == "11110");
7292
assert(binaryAddition.addBinary("101", "11") == "1000");
@@ -78,14 +98,22 @@ void tests() {
7898
"110110110110110110110110110110") ==
7999
"1100001100001100001100001100000");
80100
assert(binaryAddition.addBinary("1", "11111111") == "100000000");
81-
assert(binaryAddition.addBinary("", "") == "");
82101
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") == "");
83110
}
84111

85112
/**
86113
* Main function to execute the program.
87114
*/
88115
int main() {
89116
tests();
117+
std::cout << "All tests passed.\n";
90118
return 0;
91119
}

0 commit comments

Comments
 (0)