Thread: Help with C question.

  1. #1
    Registered User
    Join Date
    Jan 2022
    Posts
    1

    Help with C question.

    Hey, I need to make something like atoi manual from int to string. The user puts a num and then I put the num into a string manual.(in the end in the main i need to print the string %s). For example if the user enters "556" in the string there will be "556" and then I print the string. I tried to do it but I got stuck.


    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 40
    void myItoa(int num, char* str[], int size);
    
    
    void main() {
        int num1;
        char* num2 = NULL;
        printf("Please enter a number: ");
        scanf("%d", &num1);
        myItoa(num1, &num2, SIZE);
        puts(num2);
        free(num2);
    }
    
    
    void myItoa(int num, char* str[], int size) {
        int counter = 0;
        int tmp = num;
        do {
            tmp /= 10;
            counter++;
        } while (tmp);
    
    
        str = (char*)malloc(counter+1);
        if (str == NULL) {
            printf("\nAllocation failed.");
            return 0;
        }
        
        for (int i = 1, j = 10; i <= counter; i++) {
            if (counter - i == 0) {
                *(str + i) = (num / j) + 48;
            }
            *(str + i) = (num / (j * (counter - i)) + 48);
        }
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    "Help with C question" is a useless title. Obviously you want help with a C question since you posted a question in a C forum. A better title would be "Writing my own atoi function".
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define SIZE 40
     
    void myItoa(int num, char* str[], int size);
     
    // main returns int, not void
    void main() {
        int num1;
        char* num2 = NULL;
        printf("Please enter a number: ");
        scanf("%d", &num1);
        myItoa(num1, &num2, SIZE);
        puts(num2);
        free(num2);
    }
     
    // What's the point of size? You don't use it.
    void myItoa(int num, char* str[], int size) {
        int counter = 0;
        int tmp = num;
        do {
            tmp /= 10;
            counter++;
        } while (tmp);
     
        // str needs to be *str
        str = (char*)malloc(counter+1);
     
        // str needs to be *str
        if (str == NULL) {
            printf("\nAllocation failed.");
            return 0; // Your function has void return type (just say return)
        }
     
        // This algorithm is totally wrong.
        // Also, if you want to add '0' say '0', not 48.
        // Instead of *(str+i) you need to say *(*str + i), or (*str)[i]
        for (int i = 1, j = 10; i <= counter; i++) {
            if (counter - i == 0) {
                *(str + i) = (num / j) + 48;
            }
            // Presumably this should be an 'else' part of the 'if'
            *(str + i) = (num / (j * (counter - i)) + 48);
        }
        // You never '\0' terminate the string
    }
    Maybe it could be something like this:
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    void myItoa(int num, char** str);
     
    int main() {
        printf("Enter a number: ");
        int n;
        scanf("%d", &n);
     
        char *s = NULL;
        myItoa(n, &s);
        if (s) {
            puts(s);
            free(s);
        }
     
        return 0;
    }
     
    void myItoa(int num, char** str) {
        int size = 0;
        int tmp = num;
        do {
            tmp /= 10;
            ++size;
        } while (tmp);
     
        *str = malloc(size + 1);
        if (*str == NULL) {
            printf("Allocation failed.\n");
            return;
        }
     
        for (int i = size; i > 0; --i) {
            (*str)[i - 1] = num % 10 + '0';
            num /= 10;
        }
        (*str)[size] = '\0';
    }
    However, this does not handle negative integers. And it would probably be better to return a char* instead of taking a char**.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2014, 05:41 PM
  2. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  3. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  4. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM

Tags for this Thread