简体   繁体   中英

How do I convert a number to a string without using stdlib.h or string.h?

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.12 and the string 243.5 the program must create the string 2112.

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?

There are many problems in your question and your code:

  • Write a function that receives from the user two strings that show two real numbers (possibly negative), this part is unclear: do you mean the function should read user input and convert it to double values?

  • And produces a string that contains the integer part of most of the numbers represented by the string This is also unclear. From the next phrase, it appears you should output the integral part of the difference of the double values.

  • double stod(char *snum); could be implemented using sscanf() . Your version only works for positive integral values.

  • void itos(long int num, char *snum); could be implemented simply using sprintf() .

  • in addstrings you do not store the return values of stod() and instead you add the values of the first characters of the strings and you store that as the first character of the output.

  • you should never use gets()

  • printf("%d", addstring); is incorrect: you pass the address of function addstring instead of its return value.

Here is a corrected version:

#include <math.h>
#include <stdio.h>

double stod(char *snum) {
    double d;
    if (sscanf(snum, "%lf", &d) == 1)
        return d;
    else
        return 0;
}

void itos(long int num, char *snum) {
    sprintf(snum, "%ld.", num);
}

void addstring(char *snum1, char *snum2, char *res) {
    double d1 = stod(snum1);
    double d2 = stod(snum2);
    long int diff = (long int)fabs(d1 - d2);
    itos(diff, res);
}

int main() {
    char arr1[32];
    char arr2[32];
    char res[32];

    if (scanf("%31s%31s", arr1, arr2) == 2) {
        addstring(arr1, arr2, res);
        printf("%s\n", res);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM