I am traying to make program that takes from input strings s1 and s2 and then write them out in output string s3 in what are sum of numbers thar are write in s1 and s2.
for example: if is
s1='11111111'
s2='11112222' then program must sum s3 and like rezult have to get
s3='22223333'
for example: if is
s1 = '19'
s2 = '1001', then
s3 = '1020'.
But I have little trouble. Almost everything is doing just fine!
First here you are my code and after code I will put my results. And there you will see what is wrong!
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1024
void zbroji( char*, char*, char*);
int main()
{
char s1[SIZE]; char s2[SIZE]; char s3[SIZE]; int i = 0;
scanf("%s", s1);
scanf("%s", s2);
zbroji(s1, s2, s3);
while( s3[i] != '\0') /*Here need to be printf("%s", s3); but then is something wrong*/
{
printf("%d", s3[i]);
i++;
}
printf("\n");
system("PAUSE");
return 0;
}
void zbroji(char a[SIZE], char b[SIZE], char c[SIZE])
{
int i = 0, j = 0, k = 0, l = 0; int lena = 0, lenb = 0;
int BIG = 0; int salji = 0; int SMALL = 0; char TEMP[SIZE]; int temp;
while( a[lena] != '\0')
lena++;
while( b[lenb] != '\0')
lenb++;
lena -= 1; lenb -= 1;
if( lena >= lenb)
{
BIG = lena;
SMALL = lenb;
}
else
{
BIG = lenb;
SMALL = lena;
}
temp = BIG;
c[BIG + 1] = '\0';
for( i = BIG; i >= 0; i--, lena--, lenb--, BIG--)
{
if( lena < 0 )
{
j = 0;
}
else
{
if ( lenb < 0 )
{
k = 0;
}
else
{
j = a[lena] - '0'; k = b[lenb] -'0';
}
}
l = 0;
l = j + k + salji;
if( l > 9)
{
if ( BIG == 0 )
c[BIG] = l;
else
{
c[BIG] = l - 10;
salji = 1;
}
}
else
{
c[BIG] = l;
}
}
}
Code:
Output is:
s1 = 1111
s2 = 2222
s3 = 3333 that's OK!
s1 = 999
s2 = 999
s3 = 1998 that's OK!
s1 = 11
s2 = 1111
s3 = 1122 that's OK!
But when I make like this
s1 = 555
s2 = 5555
s3 = 611 WRONG!
or
s1 = 9999
s2 = 99
s3 = 10 WRONG!