C - Adding the numbers in 2 strings together if a different length - c

If I had two strings:
a = "1234"
b = "4321"
I could add the two numbers together like this:
for(i=0; i<width-1; i++){
sum = (a[width-2-i]-48) + (b[width-2-i]-48) + carry;
carry = 0;
if(sum > 9){
carry = 1;
sum-=10;
}
answer[i] = sum+48;
}
if(carry) answer[i++] = carry+48;
answer[i]= 0;
And then reverse it (width is equal to strlen(a)).
How could I do the same thing if the following?
a = "12345"
b = "4321"
Would I need to reallocate memory? Or what?
(BTW - the problem I'm trying to solve is using many numbers all with 50 digits, so strtoul or strtoull is out of the question as I understand. Here's my code so far.)

int getcharval(const char *s, int idx) {
if (idx < strlen(s))
return s[strlen(s) - idx - 1] - 48;
return 0;
}
void add() {
const char *a = "1234";
const char *b = "13210";
char answer[256];
int i, wa=strlen(a), wb=strlen(b), width, sum, carry;
width = wa > wb ? wa : wb;
for(i=0; i<width; i++){
char ca = getcharval(a, i);
char cb = getcharval(b, i);
printf("%d %d\n", ca, cb);
sum = ca + cb + carry;
carry = 0;
if(sum > 9){
carry = 1;
sum-=10;
}
answer[i] = sum+48;
}
if(carry) answer[i++] = carry+48;
answer[i]= 0;
for (i = 0; i < strlen(answer) / 2; i++) {
char t = answer[i];
answer[i] = answer[strlen(answer) - i - 1];
answer[strlen(answer) - i - 1] = t;
}
printf("%s\n", answer);
}

If you insist on using the "elementary school addition", find the length of both strings, advance to their ends, and then move back until the shorter string's length is exhausted. Then continue moving in only the longer string, assuming that the remaining digits of the shorter string are zeros:
12345
04321
You need to move all the way to the beginning of the longer string, and process the carry there. Note that you need to allocate a new result anyway, because adding two N-digit numbers may result in a N+1-digit number due to the carry.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define c2d(c) (c-'0')
#define d2c(c) (c+'0')
char* add(const char *a, const char *b, char *ans){
int alen, blen;
int i, carry=0;
char *wk;
char *awk=strdup(a);
char *bwk=strdup(b);
alen=strlen(strrev(awk));
blen=strlen(strrev(bwk));
if(alen<blen){
alen ^= blen;blen ^= alen;alen ^= blen;//swap
wk = awk ; awk = bwk ; bwk = wk;
}
ans[alen+1]=ans[alen]='\0';
for(i=0;i<alen;++i){
int sum = c2d(awk[i])+(i<blen ? c2d(bwk[i]): 0)+carry;
ans[i] = d2c(sum % 10);
carry = sum / 10;
}
if(carry){
ans[i++]='1';
}
free(awk);
free(bwk);
return strrev(ans);
}
int main(){
const char *a="12345";
const char *b="4321";
char ans[6];
printf("{%s}+{%s}={%s}\n", a, b, add(a,b, ans));
return 0;
}

cited from C - Adding the numbers in 2 strings together if a different length
answer, I write a more readable code:
void str_reverse(char *beg, char *end){
if(!beg || !end)return;
char cTmp;
while(beg < end){
cTmp = *beg;
*beg++ = *end;
*end-- = cTmp;
}
}
#define c2d(c) (c - '0')
#define d2c(d) (d + '0')
void str_add(const char* s1, const char* s2, char* s_ret){
int s1_len = strlen(s1);
int s2_len = strlen(s2);
int max_len = s1_len;
int min_len = s2_len;
const char *ps_max = s1;
const char *ps_min = s2;
if(s2_len > s1_len){
ps_min = s1;min_len = s1_len;
ps_max = s2;max_len = s2_len;
}
int carry = 0;
int i, j = 0;
for (i = max_len - 1; i >= 0; --i) {
// this wrong-prone
int idx = (i - max_len + min_len) >=0 ? (i - max_len + min_len) : -1;
int sum = c2d(ps_max[i]) + (idx >=0 ? c2d(ps_min[idx]) : 0) + carry;
carry = sum / 10;
sum = sum % 10;
s_ret[j++] = d2c(sum);
}
if(carry)s_ret[j] = '1';
str_reverse(s_ret, s_ret + strlen(s_ret) - 1);
}
test code as below:
void test_str_str_add(){
char s1[] = "123";
char s2[] = "456";
char s3[10] = {'\0'};
str_add(s1, s2, s3);
std::cout<<s3<<std::endl;
char s4[] = "456789";
char s5[10] = {'\0'};
str_add(s1, s4, s5);
std::cout<<s5<<std::endl;
char s7[] = "99999";
char s8[] = "21";
char s9[10] = {'\0'};
str_add(s7, s8, s9);
std::cout<<s9<<std::endl;
}
output:
579
456912
100020

int num(char x,int len){
if(len <0)
return 0;
return ((x=='1') ? 1 : (x=='2') ? 2 : (x=='3') ? 3 : (x=='4') ? 4 : (x=='5') ? 5 : (x=='6') ? 6 : (x=='7') ? 7 : (x=='8') ? 8 : 9);
}
int main(){
int result[100];
int i=0;
char num1[] = "123456789123456789";
char num2[] = "1234567811111111111111111111";
int carry = 0;
int l1= strlen(num1)-1;
int l2 = strlen(num2)-1;
int result1;
while(1){
if(l1 < 0 && l2 <0 && carry == 0)
break;
result1 = num(num1[l1],l1) + num(num2[l2],l2);
l1--;
l2--;
if(carry>0){
result1 +=carry;
carry = 0;
}
carry = result1 / 10;
result[i] = (result1 % 10);
i++;
}
i--;
printf("\n");
while(i>=0){
printf("%d",result[i]);
i--;
}
}

Related

Iterate Over Char Array Elements in C

This code is supposed to take a user's input and convert it to binary. The input is grouped into an integer array to store character codes and/or adjacent digits, then each item in the integer array is converted to binary. When the user types "c357", "c" should be converted to 99, then converted to binary. Then, "357" should be converted to binary as well. In the main() function, strlen(convert) does not accurately represent the number of items in array convert, thus only iterating over the first array item.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#define EIGHT_BITS 255
#define SIXTEEN_BITS 65535
#define THIRTY_TWO_BITS 4294967295UL
// DETERMINE NUMBER OF BITS TO OUTPUT
int getBitLength(unsigned long d) {
int l;
if (d <= EIGHT_BITS) {
l = 8;
}
else if (d > EIGHT_BITS && d <= SIXTEEN_BITS) {
l = 16;
}
else if (d > SIXTEEN_BITS && d <= THIRTY_TWO_BITS) {
l = 32;
}
return l;
}
// CONVERT INPUT TO BINARY VALUE
char* convertToBinary(unsigned long int decimal) {
int l = getBitLength(decimal);
static char b[33];
char bin[33];
int i, j, k = 0, r;
b[33] = '\0';
bin[33] = '\0';
printf("Bits................ %ld\n", l);
// creates array
for (i = 0; i < l; i++) {
r = decimal % 2;
decimal /= 2;
b[i] = r;
}
// reverses array for binary value
for (j = l - 1; j >= 0; j--) {
bin[k] = b[j];
strncpy(&bin[k], &b[j], l);
snprintf(&bin[k], l, "%d", b[j]);
k++;
}
printf("Binary Value: %s\n", bin);
return bin;
}
unsigned long int* numbersToConvert(char* input) {
const int MAX_INPUT = 20;
int i, k = 0, z = 0;
char numbers[MAX_INPUT];
unsigned long int *toConvert = malloc(MAX_INPUT * sizeof(int));
numbers[MAX_INPUT] = '\0';
for (i = 0; i < strlen(input); i++) {
if (isdigit(input[i])) {
numbers[z] = input[i];
if (!isdigit(input[i + 1])) {
toConvert[k] = strtol(numbers, NULL, 10);
printf("----- %ld -----\n", toConvert[k]);
z = 0;
}
else {
z++;
}
}
else {
printf("----- %c -----\n", input[i]);
printf("Character Code: %d\n", input[i]);
toConvert[k] = (unsigned long int) input[i];
}
k++;
}
return toConvert;
}
int main(void) {
const int MAX_INPUT = 20;
int i, p;
char input[MAX_INPUT];
unsigned long int* convert;
printf("------- Input --------\n");
scanf("%s", input);
input[MAX_INPUT] = '\0';
// PRINT INPUT AND SIZE
printf("\nInput: %s\n", input);
convert = numbersToConvert(input);
convert[MAX_INPUT] = '\0';
printf("strlen: %ld\n", strlen(convert));
for (i = 0; i < strlen(convert); i++) {
printf("num array: %ld\n", convert[i]);
convertToBinary(convert[i]);
}
return 0;
}
I have attempted to null terminate each string to prevent undefined behavior. I am unsure if certain variables, if any, are meant to be static.
It is hard to read your code.
Here you have something working (converting the number to binary):
static char *reverse(char *str)
{
char *end = str + strlen(str) - 1;
char *saved = str;
int ch;
while(end > str)
{
ch = *end;
*end-- = *str;
*str++ = ch;
}
return saved;
}
char *tostr(char *buff, unsigned long long val)
{
if(buff)
{
char *cpos = buff;
while(val)
{
*cpos++ = (val & 1) + '0';
val >>= 1;
}
*cpos = 0;
reverse(buff);
}
return buff;
}
int main()
{
char buff[128];
printf("%s\n", tostr(buff, 128));
}
https://godbolt.org/z/6sRC4C

How to convert to binary as string in code C

I am trying to convert int to binary as string but I can not.
Please help me. How to convert integer to binary, please tell me.
Input: 32
Output: 00100000
My code:
#include <stdio.h>
#include <string.h>
char converttobinary(int n)
{
int i;
int a[8];
char op;
for (i = 0; i < 8; i++)
{
a[i] = n % 2;
n = (n - a[i]) / 2;
}
for (i = 7; i >= 0; i--)
{
op = strcat(op, a[i]);
}
return op;
}
int main()
{
int n;
char str;
n = 254;
str = converttobinary(n);
printf("%c", str);
return 0;
}
I have tried to modify your solution with minimal changes to make it work. There are elegant solutions to convert Integer to Binary for example using shift operators.
One of the main issue in the code was you were using character instead of character array.
i.e char str; instead of char str[SIZE];
Also you were performing string operations on a single character. Additionally, iostream header file is for C++.
There is room for lot of improvements in the solution posted below (I only made your code work with minimal changes).
My suggestion is to make your C basics strong and approach this problem again.
#include <stdio.h>
#include <string.h>
void converttobinary(int n, char *op)
{
int i;
int a[8];
for (i = 0; i < 8; i++)
{
a[i] = n % 2;
n = (n - a[i]) / 2;
}
for (i = 7; i >= 0; i--)
{
op[i]=a[i];
}
}
int main()
{
int n,i;
char str[8];
n = 8;
converttobinary(n,str);
for (i = 7; i >= 0; i--)
{
printf(" %d ",str[i]);
}
return 0;
}
char *rev(char *str)
{
char *end = str + strlen(str) - 1;
char *saved = str;
while(end > str)
{
int tmp = *str;
*str++ = *end;
*end-- = tmp;
}
return saved;
}
char *tobin(char *buff, unsigned long long data)
{
char *saved = buff;
while(data)
{
*buff++ = (data & 1) + '0';
data >>= 1;
}
*buff = 0;
return rev(saved);
}
int main()
{
char x[128];
unsigned long long z = 0x103;
printf("%llu is 0b%s\n", z, tobin(x, z));
return 0;
}
I modify your code a little bit to make what you want,
the result of this code with
n = 10
is
00001010
In this code i shift the bits n positions of the imput and compare if there is 1 or 0 in this position and write a '1' if there is a 1 or a '0' if we have a 0.
#include <stdio.h>
void converttobinary(int n, char op[8]){
int auxiliar = n;
int i;
for (i = 0; i < 8; i++) {
auxiliar = auxiliar >> i;
if (auxiliar & 1 == 1){
op[7-i] = '1';
} else{
op[7-i] = '0';
}
auxiliar = n;
}
}
int main (void){
int n = 10;
int i;
char op[8];
converttobinary(n, op);
for(i = 7; i > -1; i--){
printf("%c",op[i]);
}
return 0;
}

Big number adding function crashes program

I wrote an adding function for very large numbers and when it gets called, the program crashes. I am assuming that it has to do with the carrying. Here is the code:
char * bigadd(char a[], char b[]){
int i, temp;
char useadd[MAX];
char usea = strrev(a);
char useb = strrev(b);
char ret[strlen(useadd)];
char *pa, *pb, *padd;
padd = &useadd;
pa = &usea;
pb = &useb;
while(*pa != '\0' && *pb != '\0'){
if(atoi(*pa) + atoi(*pb) + temp > 9){
if(temp + atoi(*pa) + atoi(*pb) < 20){
temp = 1;
*padd = atoi(*pa) + atoi(*pb) - 10;
}
else{
temp = 2;
*padd = atoi(*pa) + atoi(*pb) - 20;
}
}
else{
*padd = atoi(*pa) + atoi(*pb);
temp = 0;
}
++padd;
++pa;
++pb;
}
i = 0;
while(useadd[i] != '\0'){
ret[i] = useadd[i];
++i;
}
return strrev(ret);
}
Thanks for all of the help. I'm sorry if this ends up being a stupid mistake.
Your program has so many problems!
char * bigadd(char a[], char b[]){
int i, temp;
char useadd[MAX]; // MAX might not be large enough
char usea = strrev(a); // should not modify argument a
// strrev() is not standard C undefined on my system
// if defined, it returns char*, not char
char useb = strrev(b); // see above
char ret[strlen(useadd)]; // useadd is uninitialized -> undefined behaviour
char *pa, *pb, *padd;
padd = &useadd; // & is incorrect, useadd is an array
pa = &usea; // same as above
pb = &useb; // idem
// forgot to initialize temp to 0
while(*pa != '\0' && *pb != '\0'){
if(atoi(*pa) + atoi(*pb) + temp > 9){ // atoi converts a string, not a char
if(temp + atoi(*pa) + atoi(*pb) < 20){ // same... sum cannot exceed 19
temp = 1;
*padd = atoi(*pa) + atoi(*pb) - 10; // atoi...
}
else{ // never reached
temp = 2;
*padd = atoi(*pa) + atoi(*pb) - 20; // atoi...
}
}
else{
*padd = atoi(*pa) + atoi(*pb); // same atoi problem
temp = 0;
}
++padd;
++pa;
++pb;
}
// if a and b have a different size, loop fails to copy digits and propagate carry
// if temp is not 0, you forget to add the leading '1'
// trailing '\0' is not set
i = 0;
while(useadd[i] != '\0'){ // undefined behaviour, '\0' not set.
ret[i] = useadd[i];
++i;
}
// forgot to set the final '\0'
// why not use strcpy?
return strrev(ret); // strrev not defined.
// if defined, returning ret, address of local array
}
Here is a complete rewrite:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *bigadd(const char a[], const char b[]) {
int ia = strlen(a);
int ib = strlen(b);
int size = 2 + (ia > ib ? ia : ib), ic = size - 1, temp = 0;
char *res = malloc(size);
if (res) {
res[--ic] = '\0';
while (ia + ib) {
if (ia > 0) temp += a[--ia] - '0';
if (ib > 0) temp += b[--ib] - '0';
res[--ic] = '0' + temp % 10;
temp /= 10;
}
if (temp) {
memmove(res + 1, res, size - 1);
*res = '1';
}
}
return res;
}
int main(int argc, char *argv[]) {
for (int i = 1; i + 1 < argc; i += 2) {
char *res = bigadd(argv[i], argv[i+1]);
printf("%s + %s = %s\n", argv[i], argv[i+1], res);
free(res);
}
}

Adding two strings made of digits recursively in C

For this problem, I am to first take in two strings using fgets. I then need to check if the string is comprised entirely of digits thus making it a number. I was able to do this part recursively, but the next task is if the strings are numbers, I need to sum them up recursively as well. So for example,
the output of the program may look something like this:
First number > 9023905350290349
Second number > 90283056923840923840239480239480234
Sum is 90283056923840923849263385589770583
Again, I need to do this recursively, so I was thinking I could march along the stream of digits and add them together, but I am not so sure how to write this program recursively. Also since the input is in character form, I would also have to convert it to an integer, which I believe I can do by converting the individual characters to the integer ASCII value then subtracting 48 away from it. Any help would be appreciated. Thanks!
You're on the right track. Your recursive approach to checking if the input is a number looks something like the following, right? Notice that you can go ahead and subtract '0' from a character without bothering to convert it to 48 yourself.
int number_length(char *s, int pos) {
int d;
if (s[pos] == '\0') {
return pos;
}
d = s[pos] - '0';
if (d < 0 || d > 9) {
return -1;
}
return number_length(s, pos+1);
}
The above function returns -1 if the input is invalid, and the length of the number otherwise. We can use the length of the input numbers when we start the recursive addition process.
Where should the recursion begin? When we add a pair of numbers, it is convenient to start from the least significant digits.
If we have a pair of char * variables a and b pointing to the numbers, and if we know that a contains a_length digits and b contains b_length digits, then:
The least significant digit of a is at a_length-1.
The least significant digit of b is at b_length-1.
We don't know in advance how long the result is going to be, so let's build up the digits in an int * array starting from position 0. This means that we'll have the result digits in reverse, so we'll print them out starting from the end and going back to 0.
The core of the computation is this:
Given a position a_pos in a and b_pos in b, as well as a carry digit carry, compute the sum of the digits in a and b together with the carry digit.
Update the carry digit.
Add the result digit to the result array and update the length of the array.
In C, we can express the computation as follows:
d = a[a_pos--] + b[b_pos--] - 2*'0' + carry;
carry = (d >= 10 ? 1 : 0);
result[result_pos++] = d%10;
The expression a[a_pos--] + b[b_pos--] becomes invalid once a_pos or b_pos has become negative. In other words, we must deal with situations where we have run out of digits in one or both numbers. We must take care to:
Handle cases where we've already processed the most significant digit of a but not b, or b but not a.
When we've reached the end of both a and b, remember to check the carry digit: if it's 1, add it to the result and increment the length of the result.
Below is a complete implementation in ANSI C.
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 8192
char a[BUFFER_SIZE], b[BUFFER_SIZE];
int result[BUFFER_SIZE];
int number_length(char *s, int pos) {
int d;
if (s[pos] == '\0') {
return pos;
}
d = s[pos] - '0';
if (d < 0 || d > 9) {
return -1;
}
return number_length(s, pos+1);
}
int add(char *a, int a_pos, char *b, int b_pos,
int *result, int result_pos, int carry) {
int d;
if (a_pos < 0 && b_pos < 0) {
if (carry == 1) {
result[result_pos++] = 1;
}
return result_pos;
}
if (a_pos < 0) {
result[result_pos++] = b[b_pos--] - '0' + carry;
carry = 0;
} else if (b_pos < 0) {
result[result_pos++] = a[a_pos--] - '0' + carry;
carry = 0;
} else {
d = a[a_pos--] + b[b_pos--] - 2*'0' + carry;
carry = (d >= 10 ? 1 : 0);
result[result_pos++] = d%10;
}
return add(a, a_pos, b, b_pos, result, result_pos, carry);
}
int main() {
int a_length, b_length, i, result_length;
printf("First number > ");
scanf("%s", a);
if ((a_length = number_length(a, 0)) == -1) {
printf("%s is not a number.\n", a);
return 0;
}
printf("Second number > ");
scanf("%s", b);
if ((b_length = number_length(b, 0)) == -1) {
printf("%s is not a number.\n", b);
return 0;
}
result_length = add(a, a_length-1, b, b_length-1, result, 0, 0);
for (i = result_length-1; i >= 0; --i) {
printf("%d", result[i]);
}
printf("\n");
return 0;
}
UPDATE: the comment below made me realize that I've obviously misunderstood the question. My previous solution of course wouldn't have worked with huge numbers like the ones in the OP's question. I've updated my answer accordingly as "right to left" approach. The only problem is that the resulting string can have a leading zero...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void add_helper(const char *s1, const char *s2, int s1_pos, int s2_pos,
char *result, int pos, int carry) {
int d1 = 0;
int d2 = 0;
if (s1_pos >= 0) {
d1 = s1[s1_pos] - '0';
s1_pos--;
}
if (s2_pos >= 0) {
d2 = s2[s2_pos] - '0';
s2_pos--;
}
int d = d1 + d2 + carry;
carry = d > 9 ? 1 : 0;
result[pos] = '0' + (d % 10);
pos--;
if (s1_pos >= 0 || s2_pos >= 0)
add_helper(s1, s2, s1_pos, s2_pos, result, pos, carry);
else if (pos >= 0)
result[pos] = '0' + carry;
}
char *add_recurse(const char *s1, const char *s2) {
size_t s1_len = strlen(s1);
size_t s2_len = strlen(s2);
size_t result_len = (s1_len > s2_len ? s1_len : s2_len) + 1;
char *result = calloc(result_len, 1);
add_helper(s1, s2, s1_len-1, s2_len-1, result, result_len - 1, 0);
return result;
}
int main(int argc, char **argv)
{
char *num_str1 = "9023905350290349";
char *num_str2 = "90283056923840923840239480239480234";
printf("sum is %s\n", add_recurse(num_str1, num_str2));
}
Note that there is no error handling whatsoever and I assume the preconditions, that the input strings are valid strings consisting of only digits, which you said you have already checked it.
ADDED SINGLE PASS VERSION (for Jean-Baptiste Yunès, who considers the usage of 'strlen' a little bit cheating...):
int add_helper2(const char *s1, const char *s2, int acc1, int acc2,
int *s1_pos, int *s2_pos, int *pos, char **result) {
int carry = 0;
int d1 = 0;
int d2 = 0;
if (s1[acc1] || s2[acc2]) {
int t1 = (s1[acc1] != 0);
int t2 = (s2[acc2] != 0);
carry = add_helper2(s1, s2, acc1+t1, acc2+t2, s1_pos,
s2_pos, pos, result);
} else {
size_t result_len = (acc1 > acc2 ? acc1 : acc2) + 1;
*result = calloc(result_len, 1);
*s1_pos = acc1 - 1;
*s2_pos = acc2 - 1;
*pos = result_len - 1;
return 0;
}
if (*s1_pos >= 0) {
d1 = s1[*s1_pos] - '0';
*s1_pos -= 1;
}
if (*s2_pos >= 0) {
d2 = s2[*s2_pos] - '0';
*s2_pos -= 1;
}
int d = d1 + d2 + carry;
carry = d > 9 ? 1 : 0;
(*result)[*pos] = '0' + (d % 10);
*pos -= 1;
return carry;
}
char *add_recurse2(const char *s1, const char *s2) {
char *result;
int s1_pos, s2_pos, pos;
int carry = add_helper2(s1, s2, 0, 0, &s1_pos, &s2_pos, &pos, &result);
result[0] = '0' + carry;
return result;
}
Make it in a single recursive descent is not so easy, but this can make it :
char n1[] = "9023905350290349";
char n2[] = "90283056923840923840239480239480234";
char n3[1000];
char addchar(char c,char d,int r) {
return ((c-'0')+(d-'0')+r)%10 + '0';
}
int overflow(char c,char d,int r) {
return ((c-'0')+(d-'0')+r)/10;
}
int d;
int add(int i) {
if (d==0 && n1[i]!=0 && n2[i]!=0) {
int r= add(i+1);
if (d<0) {
n3[i+1] = addchar((i+d<0)?'0':n1[i+d],n2[i],r);
r = overflow((i+d<0)?'0':n1[i+d],n2[i],r);
}
if (d>0) {
n3[i+1] = addchar(n1[i],(i-d<0)?'0':n2[i-d],r);
r = overflow(n1[i],(i-d<0)?'0':n2[i-d],r);
}
if (d==0) {
n3[i+1] = addchar(n1[i],n2[i],r);
r = overflow(n1[i],n2[i],r);
}
if (i==0) {
n3[i] = r+'0';
r = 0;
}
return r;
}
if (d>=0 && n1[i]!=0) {
d++;
int r = add(i+1);
n3[i+1] = addchar(n1[i],(i-d<0)?'0':n2[i-d],r);
return overflow(n1[i],(i-d<0)?'0':n2[i-d],r);
}
if (d<=0 && n2[i]!=0) {
d--;
int r = add(i+1);
n3[i+1] = addchar((i+d<0)?'0':n1[i+d],n2[i],r);
return overflow((i+d<0)?'0':n1[i+d],n2[i],r);
}
n3[i+1] = '\0';
return 0;
}
int main() {
add(0);
printf("%s %s %s\n",n1,n2,n3);
}
The basic idea is to calculate the maximal length and the difference between lengths of numbers when descending through recursion, and then adding the right digits when returning from the recursion. The main difficulty is to manage the difference between the lengths.
This algorithm adds a leading zero to the result when there is no overflow on the left.
You could check for error in strings and do the sum at the same time,
#include <stdio.h>
#include <stdlib.h>
#define MAX_RES 1000
char *s1,*s2,*res;
int getcharval(char *s, int i) {
int n = s[i] - '0';
return n<0 || n>9 ? -1 : n;
}
char *recsum(int i1, int i2, int carry, char *pres) {
int n1 = !i1 ? 0 : getcharval(s1, --i1);
int n2 = !i2 ? 0 : getcharval(s2, --i2);
if (n1 < 0 || n2 < 0) return NULL;
int n = n1 + n2 + carry;
*--pres = (n % 10) + '0';
return !i1 && !i2 ? pres : recsum(i1, i2, n/10, pres);
}
with s1 points to string 1, s2 points to string 2, res points to the result area.
The recursive function recsum does the work, taking i1 decreasing index to next char in s1, i2 decreasing index to next char in s2, carry is the result from the previous calculation and pres (p-res) points to the next result char (+1) in res.
The helper function getcharval gets the digit from strings s index i, and returns that number (0 to 9) or -1 if the character is not a digit.
recsum returns a pointer to the result, i.e. a pointer into res where the result starts.
If there was an error in either string, the function returns NULL instead.
An example as how to use recsum, for a result having 1000 chars max (MAX_RES)
int main (int argc, char **argv)
{
s1 = "02313123";
s2 = "92382472699";
res = malloc(MAX_RES+1);
res[MAX_RES] = 0;
char *ret = recsum(strlen(s1), strlen(s2), 0, res+MAX_RES);
if (!ret) printf("There is an error\n");
else printf("%s + %s = %s\n", s1, s2, ret);
return 0;
}
Since If think this is homework, I only show pseudocode.
def str_sum(a,b):
index_a = len(a)
index_b = len(b)
res_len = max(len(a), len(b))
result = calloc(res_len+2, 1)
if not result:
raise OutOfMemory()
index_a -=1
index_b -= 1
acc = 0
res_index = 0
while (index_a >=0) or (index_b >= 0):
chr_a = '0'
chr_b = '0'
if(index_a >=0):
chr_a = a[index_a]
if(index_b >=0):
chr_b = b[index_b]
temp = acc + ord(chr_a) - ord('0') + ord(chr_b) - ord('0')
result[res_index] = chr((temp % 10) + ord('0'))
acc = temp / 10
index_a -=1
index_b -= 1
res_index += 1
inplace_rewind(result)
return ''.join(result)
print str_sum('9023905350290349', '90283056923840923840239480239480234')

C Code for Hexadecimal without using standard libraries

I want to write C Code which converts first string into integer then integer into hexadecimal.
ex: I have Ip iddress as "172.24.18.240" now first find out first dot from it and take the number before it that is "172" convert it into integer then convert it inti hexadecimal and it should do the same for all like 24,18,240 and convert into long/integer value
any help is appreciated.
#include <stdio.h> // testing
int main(int argc, char** argv) // testing
{
char* ipString = argc > 1? argv[1] : "172.24.18.240"; // testing
char* ip = ipString;
unsigned int hex;
for( int i = 0; i < 4; i++ ){
unsigned int n = 0;
for( char c; (c = *ip) >= '0' && c <= '9'; ip++ )
n = 10 * n + c - '0';
hex = (hex << 8) + n;
if( *ip == '.' ) ip++;
}
printf("%08X\n", hex); // testing
return 0; // testing
}
Maybe something like this?
char sn[4];
char *nid = hexString;
int nip[4];
int xnip[4];
int j = 0;
while (*nid != '\0') {
int i = 0;
memset(sn, '\0', sizeof sn);
while (isdigit(*nid)) {
sn[i++] = *nid++;
}
if (*nid == '.')
nid++;
// now sn should be the number part
nip[j] = your_str_to_int(sn);
xnip[j] = your_int_to_hex(nip[j]);
j++;
}
int main(void)
{
char hexChars[] = "0123456789ABCDEF";
char ipString[] = "172.24.18.254";
char hexString[9] = "";
const char* pch = ipString;
int num = 0;
int i = 0;
do
{
if (*pch != '.' && *pch != '\0')
{
num *= 10;
num += (*pch - '0');
}
else
{
hexString[i++] = hexChars[num / 16];
hexString[i++] = hexChars[num % 16];
num = 0;
}
} while (*pch++);
return 0;
}
The hex values will stored in hexString.
int i = 0, sum = 0;
char ipString[] = "172.24.18.240";
do
{
if (isdigit(ipString[i])) sum = sum * 10 + ipString[i] - '0';
else
{ putchar("0123456789ABCDEF"[sum / 16]);
putchar("0123456789ABCDEF"[sum % 16]);
putchar('.');
sum = 0;
}
}
while (ipString[i++] != '\0');
More or less ugly, but should work on IP addresses.

Resources