forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadding_two_string.cpp
More file actions
59 lines (50 loc) · 1.13 KB
/
Copy pathadding_two_string.cpp
File metadata and controls
59 lines (50 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Program : To add 2 string
this Program is Contributed by github@b419007
*/
#include <iostream>
using namespace std;
int Len(string &str1, string &str2)
{
int len1 = str1.size();
int len2 = str2.size();
if (len1 < len2)
{
for (int i = 0 ; i < len2 - len1 ; i++)
str1 = '0' + str1;
return len2;
}
else if (len1 > len2)
{
for (int i = 0 ; i < len1 - len2 ; i++)
str2 = '0' + str2;
}
return len1;
}
string add( string a, string b )
{
string result;
int len = Len(a, b);
int carry = 0;
for (int i = len-1 ; i >= 0 ; i--)
{
int aBit = a.at(i) - '0';
int bBit = b.at(i) - '0';
int sum = (aBit ^ bBit ^ carry)+'0';
result = (char)sum + result;
carry = (aBit & bBit) | (bBit & carry) | (aBit & carry);
}
if (carry)
result = '1' + result;
return result;
}
int main()
{
string str1,str2;
cout<<"Enter the string 1 :";
cin>>str1;
cout<<"Enter the string 2 :";
cin>>str2;
cout << "Sum is " << add(str1, str2);
return 0;
}