C++ Program To Add Two Binary Strings Last Updated : 17 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given two binary strings, return their sum (also a binary string).Example: Input: a = "11", b = "1" Output: "100" We strongly recommend you to minimize your browser and try this yourself first The idea is to start from the last characters of two strings and compute the digit sum one by one. If the sum becomes more than 1, then store carry for the next digits. C++ // C++ program to add two // binary strings #include <bits/stdc++.h> using namespace std; // This function adds two binary strings // and return result as a third string string addBinary(string A, string B) { // If the length of string A is greater // than the length of B then just swap // the string by calling the same // function and make sure to return // the function otherwise recursion // will occur which leads to calling // the same function twice if (A.length() > B.length()) return addBinary(B, A); // Calculating the difference between // the length of the two strings. int diff = B.length() - A.length(); // Initialise the padding string which // is used to store zeroes that should // be added as prefix to the string which // has length smaller than the other string. string padding; for (int i = 0; i < diff; i++) padding.push_back('0'); A = padding + A; string res; char carry = '0'; for (int i = A.length() - 1; i >= 0; i--) { // This if condition solves 110 111 // possible cases if (A[i] == '1' && B[i] == '1') { if (carry == '1') res.push_back('1'), carry = '1'; else res.push_back('0'), carry = '1'; } // This if condition solves 000 001 // possible cases else if (A[i] == '0' && B[i] == '0') { if (carry == '1') res.push_back('1'), carry = '0'; else res.push_back('0'), carry = '0'; } // This if condition solves 100 101 010 // 011 possible cases else if (A[i] != B[i]) { if (carry == '1') res.push_back('0'), carry = '1'; else res.push_back('1'), carry = '0'; } } // If at the end their is carry then just // add it to the result if (carry == '1') res.push_back(carry); // reverse the result reverse(res.begin(), res.end()); // To remove leading zeroes int index = 0; while (index + 1 < res.length() && res[index] == '0') index++; return (res.substr(index)); } // Driver code int main() { string a = "1101", b = "100"; cout << addBinary(a, b) << endl; return 0; } Output: 10001 Time Complexity: O(max(L1, L2)), where L1 and L2 are the lengths of strings a and b respectively. Auxiliary Space: O(max(L1, L2)), where L1 and L2 are the lengths of strings a and b respectively. Comment More infoAdvertise with us Next Article C++ Program To Add Two Binary Strings kartik Follow Improve Article Tags : Strings Mathematical C++ Programs C++ DSA CPP Strings Programs +2 More Practice Tags : CPPMathematicalStrings Similar Reads C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad 1 min read How to Add Leading Zeros to a C++ String? In C++, a string data structure is used to store the sequence of characters. These characters can be letters, symbols, or numbers. In this article, we will learn how to add leading zeros to a string that represents some numeric data in C++. Examples: Input: str="123" N=2 Output: "00123" Explanation: 2 min read C++ Program For String to Double Conversion There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun 3 min read C++ Program to concatenate two strings using Operator Overloading Pre-requisite: Operator Overloading in C++Given two strings. The task is to concatenate the two strings using Operator Overloading in C++. Example: Input: str1 = "hello", str2 = "world" Output: helloworld Input: str1 = "Geeks", str2 = "World" Output: GeeksWorld Approach 1: Using unary operator overl 3 min read C++ Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1- 12 min read Like