Hey,
I was trying to solve a problem that required to add one hundred 50 digit numbers. Since there is no way to hold such a huge number. I read that storing them in strings is the way to go. Unfortunately, I was caught midway while trying to do the same.
Here's the source code;
Code:
#include<stdio.h>
#include<string.h>
const char * addTwoStrings(char *number1,char *number2)
{
int i = strlen(number1);
printf("%s\n",number1);
printf("%d\n",i);
return "hello";
}
int main(void)
{
char filename[] = "inputNumbers.txt";
FILE *FP;
char c;
int i=0,j=0;
char string[2][9] = {'\0'};
FP = fopen(filename,"r");
if(FP ==NULL)
{
printf("Error opening file");
return 0;
}
while((c= fgetc(FP)) != EOF)
{
if(c == '\n')
{
i++;
j=0;
continue;
}
string[i][j++] = (char)c;
}
addTwoStrings(string[0],string[1]);
/* for(i=0;i<2;i++)
{
for(j=0;j<9;j++)
{
printf("%d",string[i][j]-'0');
}
printf("\n");
}*/
return 0;
}
And the text file is this.
Code:
123465789
321654987
This isn't the exact huge number, but I wanted to try it out with lower number before trying out with the original huge ones.
As you can see, I am trying to store the numbers in a two-dimensional array. However when I and try to pass the single number as an parameter to the AddTwoStrings() method, It actually passes the entire number as such.
When I pass string[0],string[1] it should pass the first and second number from the files as the two numbers instead of the whole number as such.
The function AddTwoStrings() doesn't do anything as of now, I encountered this error when I was testing the code till this part.
Thanks.