#include <stdio.h>
#include <stdlib.h>
// Function to convert integer to string
char* intToStr(int num);
int main() {
int number = 12345;
char* strNumber = intToStr(number);
printf("Integer: %d\nString: %s\n", number, strNumber);
// Free the allocated memory
free(strNumber);
return 0;
}
char* intToStr(int num) {
// Determine the number of digits in the integer
int temp = num;
int numDigits = 0;
while (temp != 0) {
temp /= 10;
numDigits++;
}
// Allocate memory for the string, including space for the null terminator
char str = (char)malloc((numDigits + 1) * sizeof(char));
// Check if memory allocation was successful
if (str == NULL) {
exit(EXIT_FAILURE);
}
// Convert the integer to string by extracting digits
for (int i = numDigits - 1; i >= 0; i--) {
str[i] = (num % 10) + '0';
num /= 10;
}
// Add null terminator at the end of the string
str[numDigits] = '\0';
return str;
}