C program to implement your own itoa function

  • Write a program in C to implement your own itoa function.
  • How to convert integer to string in C programming language.

Itoa function converts an integer value(positive and negative both) to a string. here is the function prototype of itoa function:

char* itoa(int val, char* string, int base);

It converts val(integer) to a string and stores it in string(character array) and returns the same. It also take base of the number as third parameter, If we pass base 2 then itoa will convert passed integer to equivalent binary number and stores it in string.

For Example:

 "1234" = itoa(1234, string, 10);
 "-1234" = itoa(-1234. string. 10);
"10011010010" = itoa("1234", string, 2);

C program to implement our own itoa function

C program to implement your own itoa function

#include <stdio.h>
  
void my_reverse(char str[], int len);
char* my_itoa(int num, char* str, int base);
 
int main() {
    int i, b;
    char charArray[128];
    printf("Enter a number and base\n");
    scanf("%d %d", &i, &b);
     
    printf("String : %s", my_itoa(i, charArray, b));
    return 0;
}
 
/*
* function to reverse a string 
*/
void my_reverse(char str[], int len)
{
    int start, end;
    char temp;
    for(start=0, end=len-1; start < end; start++, end--) {
        temp = *(str+start);
        *(str+start) = *(str+end);
        *(str+end) = temp;
    }
}
 
char* my_itoa(int num, char* str, int base)
{
    int i = 0;
    bool isNegative = false;
  
    /* A zero is same "0" string in all base */
    if (num == 0) {
        str[i] = '0';
        str[i + 1] = '\0';
        return str;
    }
  
    /* negative numbers are only handled if base is 10
       otherwise considered unsigned number */
    if (num < 0 && base == 10) {
        isNegative = true;
        num = -num;
    }
  
    while (num != 0) {
        int rem = num % base;
        str[i++] = (rem > 9)? (rem-10) + 'A' : rem + '0';
        num = num/base;
    }
  
    /* Append negative sign for negative numbers */
    if (isNegative){
        str[i++] = '-';
    }
  
    str[i] = '\0';
 
    my_reverse(str, i);
  
    return str;
}

Output

Enter a number and base
1234 10
String : 1234
Enter a number and base
-1234 10
String : -1234
Enter a number and base
10 2
String : 1010