/*
* GetInt.c
*
* Created by Ljubomir Gorscak
* 21/09/09.
* Seneca Learn Id : gljubomir
* OOP344 - Fall 2009
*
* Challenge: write this function without
* using any library functions;
* void GetInt(char *strint, int val);
* this function gets an integer value and
* converts it to a string ("strint")
*
* My lean and mean version has lots of
* notes but very little code. I include
* a main()and two additional functions
* for display.
*
* I changed the brackets so that the code
* will be easier to read on this blog.
*/
#include
#define MAX 6
void GetInt(char *, int);
void PrintStrint(const char *);
void PrintMAX();
int main()
{
int val;
char strint[MAX];
printf("Enter an integer less than ");
PrintMAX();
scanf("%d", &val);
printf("Your integer is: %d\n", val);
GetInt(strint, val);
strint[MAX] = '\0';
printf("The string version is: ");
PrintStrint(strint);
return 0;
} /* end of main */
/* this function gets an integer value
* and converts it to a string ("strint")
*/
void GetInt(char *strint, int val)
{
int i;
int mod_int;
mod_int = val % 10;
val /= 10;
for (i = MAX - 1; i >= 0; i--)
{
strint[i] = mod_int + 48;
mod_int = val % 10;
val /= 10;
}
} /* end of GetInt(...) */
/* this function is used to print the string
* version of the number without the leading
* zeros
*/
void PrintStrint(const char *strint)
{
int i = 0;
for ( ; i < MAX; i++)
{
if(i < MAX && strint[i] != '0')
for ( ; i < MAX && printf("%c", strint[i]); i++);
}
printf("\n");
} /* end of PrintStrint(...) */
/* this function is used to inform the user
* about the value of #define MAX so that
* the user does not exceed MAX input
*/
void PrintMAX()
{
int i = 0;
for ( ; i < MAX && printf("9"); i++);
printf(": ");
} /* end of PrintMAX() */
/*
* end
*/