Here is my exercise:
Write a function that receives from the user two strings that show two real numbers (possibly negative), And produces a string that contains the integer part of most of the numbers represented by the string. For example, Given the string
2356.12and the string243.5the program must create the string2112.
void addstrings(char *snum1, char *snum2, char *res);
I need to create this function, and two more helper functions
// a function that receives a string that represents an actual number and returns the above number (that i succeed)
double stod(char *snum);
// A function that receives an integer and produces a string representing the number
void itos(long int num, char *snum);
that are used in the addstrings function.
Here is my attempt so far:
void addstring(char *snum1, char *snum2, char *res) {
stod(snum1);
stod(snum2);
*res = (long int)(*snum1 + *snum2);
}
double stod(char *snum) {
int res = 0;
for (int i = 0; snum[i] != '\0'; i++)
res = res * 10 + snum[i] - '0';
// return result.
return res;
}
double itos(long int num, char *snum) {
int i = 0;
while (num) {
snum[i++] = (num % 10) + '0';
num = num / 10;
}
return (double)num;
}
int main() {
char arr1[SIZE + 1];
char arr2[SIZE + 1];
char *res = NULL;
int temp;
gets(arr1);
gets(arr2);
addstring(arr1, arr2, res);
printf("%d", addstring);
return 0;
}
What am I missing in the addstring function?