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);
}
}
Related
hi i have been trying to convert a string map to an 2d array but the more i change the more i get confused. now it does not compile and complains its = NUL (edited i added malloc and seems that and gives segmentation fault).
#include <unistd.h>
#include <stdlib.h>
char **stoa(char *str)
{
int i;
int j;
int k;
char **map;
//int len = ft_strlen_line(str);
//int row = ft_strlen(str);
i = 0;
j = 0;
k = 0;
//arow = row/len;
//map = malloc(j * k sizeof(char*));
map = malloc(j * sizeof(k));
while(str[i])
{
if (str[i] == '\n')
{
k = 0;
j++;
}
map[j][k] = str[i];
k++;
i++;
}
free(map);
return (map);
if (!(map = malloc(j * k * sizeof(char))))
return NULL;
}
int main(void)
{
char row12[] = "ABABABABAB\nABABABABAB\nABABABABAB\n\0";
char **map;
map = stoa(row12);
return (0);
}
Not tested.
char **stoa(char *str)
size_t nlines = 0;
size_t pos = 0;
char **array = NULL;
char **tmp;
while(*str)
{
while(*str && str[pos++] != '\n');
nlines++;
tmp = realloc(array, (nlines + 1) * sizeof(*array));
if(tmp) array = tmp;
else break;
array[nlines] = NULL;
if(array[nlines - 1] = malloc(pos + 2));
if(array[nlines - 1])
{
memcpy(array[nlines - 1], str, pos);
array[nlines - 1][pos] = 0;
str = str + pos;
if(str == '\n') str++;
pos = 0;
}
else break;
}
return array;
}
I need to convert Integer to Char, I can use only pointers without array indexes. Char array must be dynamically allocated. Can anybody review my code and tell my what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main(){
int myNumber = 1234;
char *myString = (char*)malloc(2 * sizeof(char)); //memory for 1 char and '\0'
int i = 0; //parameter for tracking how far my pointer is from the beggining
if (myNumber < 0){
*myString = '-'; //if myNumber is negative put '-' in array
*myString = *(myString + 1); //move pointer to next position
i++;
}
while (myNumber != 0){
myString = (char*)realloc(myString, (i + 2) * sizeof(char)); //increse memory =+1
*myString = (myNumber % 10) + '0'; //put digit as char to array
myNumber /= 10;
*myString = *(myString + 1); //move pointer to next position
i++;
}
*myString = '\0'; //mark end of string
*myString = *(myString - i); //move pointer back to the beggining of string
printf("\n%s", *myString); // print char array (not working..)
return 0;
}
I know there are better ways of converting Int to String (sprintf), but my task is to do it that way.
In the code above I`m taking the digits from Int backwards, can it be done in correct order?
edit. as mentioned in the comments the part:
*myString = *(myString + 1);
is wrong, correct way of redirecting pointer by one space is:
myString++;
same with:
*myString = *(myString - i); //wrong
myString -=i; //ok
Edit2:
Now my code works! But I need to think how to correct the order of the digits.
#include <stdio.h>
#include <stdlib.h>
int main(){
int myNumber = 1234;
char *myString = (char*)malloc(2 * sizeof(char)); //memory for 1 char and '\0'
char * position = myString;
int i = 0;
if (myNumber < 0){
*position = '-'; //if myNumber is negative put '-' in array
position += i; //move pointer to next position
i++;
}
while (myNumber != 0){
myString = (char*)realloc(myString, ((i + 2) * sizeof(char))); //increse memory =+1
position = myString + i; // getting current position after reallocating
*position = (myNumber % 10) + '0'; //put digit to array
myNumber /= 10;
position++; //move pointer to next position
i++;
}
*position = '\0'; //mark end of string
char * temp = myString;
while (*temp != '\0'){
printf("%c", *temp); // print char array (not working..)
temp++;
}
return 0;
}
Edit 3 Problem solved, thanks for comments, I`m posting code in case anybody will look for similar solution.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// move each character in array one place to the right
// we need to make place for new character on the left
void moveArrayElementsRight(char *ptr, int len) {
for (int j = len; j > 1; j--) {
*(ptr + j - 1) = *(ptr + j - 2);
}
}
void intToStr(int myNumber, char* myString){
int i = 1; //track size of allocated memory
bool isMinus = false;
if (myNumber < 0) {
myNumber *= -1; //without this (myNumber % 10) + '0' wont work
isMinus = true;
}
if (myNumber == 0){ //special case for 0
myString = (char*)realloc(myString, ((i + 1) * sizeof(char)));
*myString = '0';
*(myString + 1) = '\0';
}
while (myNumber != 0) {
myString = (char*)realloc(myString, ((i + 1) * sizeof(char))); //increse memory =+1 for next digit
i++;
moveArrayElementsRight(myString, i);
*myString = (myNumber % 10) + '0'; //put digit to array
myNumber /= 10;
}
if (isMinus) {
myString = (char*)realloc(myString, ((i + 1) * sizeof(char))); //increse memory =+1 for '-' sign
i++;
moveArrayElementsRight(myString, i);
*myString = '-'; //put sign at the beginning
}
}
int main() {
int numberToConvert = -10;
char *numberAsString = (char*)malloc(sizeof(char)); //create empty array, with place only for '\0'
*numberAsString = '\0'; //mark the end of array
intToStr(numberToConvert, numberAsString);
printf("%s", numberAsString);
return 0;
}
SPOILER: Don't read or copy this if you don't want the solution.
The following is an example of implementation, using recursive:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include <errno.h>
static size_t int_to_str_size(int n, size_t acc, int base_size) {
int next = n / base_size;
if (next != 0) {
return int_to_str_size(next, acc + 1, base_size);
} else {
return n < 0 ? acc + 2 : acc + 1;
}
}
static void int_to_str_write(int n, char *str, char *base, int base_size) {
*str = base[abs(n % base_size)];
int next = n / base_size;
if (next != 0) {
int_to_str_write(next, str - 1, base, base_size);
} else {
if (n < 0) {
*(str - 1) = '-';
}
}
}
static char *int_to_str(int n, char *base) {
size_t base_size = strlen(base);
if (base_size < 2 || base_size > INT_MAX) {
errno = EINVAL;
return NULL;
}
size_t size = int_to_str_size(n, 0, (int)base_size);
char *str = malloc(size + 1);
if (str == NULL) {
return NULL;
}
str[size] = '\0';
int_to_str_write(n, str + size - 1, base, (int)base_size);
return str;
}
int main(void) {
srand((unsigned int)time(NULL));
for (uintmax_t i = 0; i < 42; i++) {
int n = rand() % 2 ? rand() : -rand();
char *str = int_to_str(n, "0123456789");
if (str != NULL) {
printf("%d == %s\n", n, str);
free(str);
} else {
perror("int_to_str()");
}
}
}
I need to change the text in array in a way, that everywhere I have a character, I need to change it to123.
Example: for the given text: ayasxka I should got this: 12123k123 or this 12323k123 text.
I almost got it to work, but instead of k between numbers, I got s, I mean, this is my result: 12123s123.
int main()
{
int i, j = 0;
char t[] = "ayasxka";
char *r = malloc(sizeof(char) * (strlen(t) + 2));
memset(r, '\0', (strlen(t) + 1));
for(i=0; t[i] != '\0'; i++)
{
if(t[i] == 'a')
{
r[i] = '1';
r[i+1] = '2';
r[i+2] = '3';
}
else
r[i+2] = t[i];
}
printf("%s\n", r);
free(r);
return 0;
}
In your algorithm you should use:
for(i=0; t[i] != '\0'; i++)
{
if(t[i] == 'a')
{
j = i;
r[j++] = '1';
r[j++] = '2';
r[j++] = '3';
}
else if(j==i)
r[j++] = t[i];
}
r[j] = '\0';
and in your malloc you should add +1 character for the '\0', because strlen() doesn't count that, so
char *r = malloc(3 + strlen(t));
instead of
char *r = malloc(2 + strlen(t));
This will give you 12123k123
The problem is strlen(t) + 2 is not enough, consider the worst case i.e. the string is just made of only a characters, then it should be
char *r = malloc(3 * strlen(t) + 1);
and szieof(char) == 1 is madatory by the way.
And you will need a counter for the position in the r string, say j
Try this
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
int i, j = 0;
char t[] = "ayasxka";
/* don't call strlen multiple times, store the value if it wont change */
size_t length = strlen(t);
/* it doesn't matter how unlikely malloc will fail, check that + */
char *r = malloc(3 * length + 1); /* | */
if (r == NULL) /* <--------------------------------------------+ */
return -1;
for (i = 0 ; t[i] != '\0' ; i++)
{
if(t[i] == 'a')
{
r[j++] = '1';
r[j++] = '2';
r[j++] = '3';
}
else
r[j++] = t[i];
}
/* you don't need the memset */
r[j] = '\0';
printf("%s\n", r);
free(r);
return 0;
}
int i;// j = 0;//unused `j`
char t[] = "ayasxka";
char *r = calloc(strlen(t) + 2 + 1, sizeof(char));//change size, +2: for last a, +1: for NUL
//memset(r, '\0', (strlen(t) + 1));//calloc initialize by 0
for(i=0; t[i] != '\0'; i++){
if(t[i] == 'a'){
r[i] = '1';
r[i+1] = '2';
r[i+2] = '3';
}
else if(r[i] == '\0'){//Not yet been set to the value
r[i] = t[i];
}
}
printf("%s\n", r);//12123k123
free(r);
char t[] = "ayasxka";
int i, len = strlen(t);
char *r = calloc(len + 2 + 1, sizeof(char));
for(i=len-1; i>=0; --i){
if(t[i] == 'a'){
r[i] = '1';
r[i+1] = '2';
r[i+2] = '3';
}
else if(r[i] == '\0'){
r[i] = t[i];
}
}
printf("%s\n", r);//12323k123
free(r);
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--;
}
}
I'm trying to find sum of large numbers.(100-digit,1500-digit)
There is my sum function:
char *find_sum(char *a, char *b) {
char *res;
int alen, blen, rlen;
int carry;
alen = strlen(a);
blen = strlen(b);
rlen = 1 + ((alen > blen) ? alen : blen);
res = malloc(1 + rlen);
if (res) {
int oldlen = rlen;
res[rlen] = 0;
carry = 0;
while (rlen) {
int tmp;
if (alen && blen) tmp = a[--alen] - '0' + b[--blen] - '0';
else if (alen) tmp = a[--alen] - '0';
else if (blen) tmp = b[--blen] - '0';
else tmp = 0;
tmp += carry;
res[--rlen] = '0' + tmp % 10;
carry = tmp / 10;
}
if (res[0] == '0') memmove(res, res+1, oldlen);
}
return res;
}
If I'm try to like below, code is working:
char a[] = "243432423423423";
char b[] = "74356348775345";
char *c;
c = find_sum(a,b);
printf("%s",c);
But I want to these numbers(a and b) get from a file(by line).For example my data.txt has below lines:
7326473264723672364723864762374236
32473264623748632784632784
432423432423423423
0
3248972389473289473289478923
4897238473247382
732468723647236478238423
0
432748932489327894723894798239
48327489237483278
0
32423423423423
I want to open this file, read each line and sum of all numbers(stopped if reach 0 and write other file sum.txt)
If I try to addition values from files using fgets, I take the incompatible types error.
My testing code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *find_sum(char *a, char *b);
int main(int argc, const char *argv[])
{
FILE *file;
file = fopen("a.txt", "r");
if (file != NULL){
char *buf;
char *buf1;
char *sum;
fgets(buf, 100, file);
fgets(buf1, 100, file);
sum = find_sum(buf, buf1);
printf("%s",sum);
}
fclose(file);
return 0;
}
char *find_sum(char *a, char *b) {
char *res;
int alen, blen, rlen;
int carry;
alen = strlen(a);
blen = strlen(b);
rlen = 1 + ((alen > blen) ? alen : blen);
res = malloc(1 + rlen);
if (res) {
int oldlen = rlen;
res[rlen] = 0;
carry = 0;
while (rlen) {
int tmp;
if (alen && blen) tmp = a[--alen] - '0' + b[--blen] - '0';
else if (alen) tmp = a[--alen] - '0';
else if (blen) tmp = b[--blen] - '0';
else tmp = 0;
tmp += carry;
res[--rlen] = '0' + tmp % 10;
carry = tmp / 10;
}
if (res[0] == '0') memmove(res, res+1, oldlen);
}
return res;
}
I gess you are gettign this error
36 D:\Projects\c_c++\so\find_sum\main.cpp invalid conversion from `void*' to `char*'
at the line
res = malloc(1 + rlen);
malloc returns void* witch yo need to cast to the type for witch you are allocating the memory. to get rid of this error add a type cats this way
res = (char*) malloc(1 + rlen);
your code will compile then.
EDIT
Read numbers from file and write result of addition to another output file
Basically you will need to that in a loop, read two operands and make the addition, since you have to stop adding only after you have found a line containing a "0" or you have reached EOF, you'll have to read the next operand in the next loop and add the sum from previous loop to it then store the result back to the sum. after you have reached "0" or EOF write the file sum to the output file.
an other alternative would be to read all numbers into an array of strings till you reach "0" or EOF in one step, an in the next step you iterate over all numbers red, compute the sum and then write it to the output file.
here is a sample implementation for the first solution
int main(int argc, const char *argv[])
{
char buf[100] = "";
FILE *input_file = fopen("a.txt", "r");
if (input_file) {
FILE *output_file = fopen("r.txt", "w");
if(output_file) {
char *op1 = NULL, *op2 = NULL, *sum = NULL, *p_buf = NULL;
do {
// if we alredy have done an additin, copy (flat) that result to op1
if(sum) {
printf("have sum %s\n", sum);
op1 = sum;
}
// if op1 does not point to a sum from previous addition, then attemp to read it from file
if(! op1) {
// read next operand and escape all "0"
do {
p_buf = fgets(buf, 100, input_file);
remove_new_line_ending(p_buf, buf);
} while(p_buf && 0 == strcmp(p_buf, "0"));
if(p_buf) {
printf("read op1 %s\n", buf);
op1 = strdup(buf);
sum = op1;
}
}
// read next operand
p_buf = fgets(buf, 100, input_file);
remove_new_line_ending(p_buf, buf);
if(p_buf && 0 != strcmp(p_buf, "0")) {
printf("read op2 %s\n", buf);
op2 = strdup(buf);
}
// we have both op1 and op2 then make the addition
if(op1 && op2) {
printf("have op1 and op2 %s\n", "");
sum = find_sum(op1, op2);
} else {
if(sum) {
// if we have only op1 then it is the result from the previous addion and there is no operand left in the file
// then write the result to output file and reset all variables
printf("print sum %s to output file\n\n", sum);
fprintf(output_file, "%s\n0\n", sum);
free(sum);
sum = NULL;
}
}
free(op1);
free(op2);
op1 = NULL;
op2 = NULL;
} while(p_buf);
fclose(output_file);
} else {
perror("r.txt");
}
fclose(input_file);
} else {
perror("a.txt");
}
return 0;
}
void remove_new_line_ending(char* line, char dest[])
{
if(line) {
int len = strlen(line);
int i = 0;
while(i < len && line[i] != '\r' && line[i] != '\n') {
dest[i] = line[i];
i++;
}
dest[i] = '\0';
}
}
Input
0
0
7326473264723672364723864762374236
32473264623748632784632784
432423432423423423
0
0
0
3248972389473289473289478923
4897238473247382
732468723647236478238423
0
0
432748932489327894723894798239
48327489237483278
0
32423423423423
0
0
Output
7326473297196937420895929970430443
0
3249704858201833948240964728
0
432748932489376222213132281517
0
32423423423423
0
You should declare your variables at the beginning of the main function (not inside a conditional), and you must allocate memory to them. The easiest way to do this would be to declare them as
int main(int argc, const char *argv[])
{
char buf[100];
char buf1[100];