text
stringlengths 0
82
|
---|
// Function to find the sum of larger |
// numbers represented as a string |
char* findSum(const char* str1, const char* str2) |
, |
// Before proceeding further, make |
// sure length of str2 is larger |
if (strlen(str1) > strlen(str2)) , |
const char* temp = str1; |
str1 = str2; |
str2 = temp; |
- |
// Stores the result |
static char str*1000+; |
// Calculate length of both strings |
int n1 = strlen(str1); |
int n2 = strlen(str2); |
// Reverse both of strings |
strrev(str1); |
strrev(str2); |
int carry = 0; |
for (int i = 0; i < n1; i++) , |
// Find the sum of the current |
// digits and carry |
int sum |
= ((str1*i+ - '0') |
+ (str2*i+ - '0') |
+ carry); |
str*i+ = (sum % 10) + '0'; |
// Calculate carry for next step |
carry = sum / 10; |
- |
// Add remaining digits of larger number |
for (int i = n1; i < n2; i++) , |
int sum = ((str2*i+ - '0') + carry); |
str*i+ = (sum % 10) + '0'; |
carry = sum / 10; |
- |
// Add remaining carry |
if (carry) |
str*n2+ = carry + '0'; |
else |
str*n2+ = '\0'; |
// Reverse resultant string |
strrev(str); |
return str; |
- |
// Function to find difference of larger |
// numbers represented as strings |
char* findDiff(const char* str1, const char* str2) |
, |
// Stores the result of difference |
static char str*1000+; |
// Calculate length of both strings |
int n1 = strlen(str1), n2 = strlen(str2); |
// Reverse both of strings |
strrev(str1); |
strrev(str2); |
int carry = 0; |
// Run loop till small string length |
// and subtract digit of str1 to str2 |
for (int i = 0; i < n2; i++) , |
// Compute difference of the |
// current digits |
int sub |
= ((str1*i+ - '0') |
- (str2*i+ - '0') |
- carry); |
// If subtraction < 0 then add 10 |
// into sub and take carry as 1 |
if (sub < 0) , |
sub = sub + 10; |
carry = 1; |
- |
else |
carry = 0; |
str*i+ = sub + '0'; |
- |
// Subtract the remaining digits of |
// larger number |
for (int i = n2; i < n1; i++) , |
int sub = ((str1*i+ - '0') - carry); |
// If the sub value is -ve, |
// then make it positive |
if (sub < 0) , |
sub = sub + 10; |
carry = 1; |
- |
else |
carry = 0; |
str*i+ = sub + '0'; |
- |
// Reverse resultant string |
strrev(str); |
// Return answer |
return str; |
- |
// Function to remove all leading 0s |
// from a given string |
char* removeLeadingZeros(char* str) |
, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.