Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 13, 2017 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/aabfb2246e0a80e89d78c3a919cb9757 to your computer and use it in GitHub Desktop.
Save jianminchen/aabfb2246e0a80e89d78c3a919cb9757 to your computer and use it in GitHub Desktop.
Convert an integer to its alpha-numeric equivalent represented as a null terminated string
Write a function that converts an int into its alpha-numeric equivalent represented as a null terminated string.
The function should accept an int as input and return a string as output. For instance, calling the function with
an int value of 324 would return a null terminated string containing "324". Ensure that your function checks for
appropriate boundary conditions and edge cases. Assume you cannot use any standard libraries (for example, no itoa or sprintf).
// Let me spend first 10 minutes to model this question
// Convert an int to alpha-numeric string
// let me think about module function 324%10=4, we get first digit, but in reverse order; continue, %10, get second
// digit, 324/10 as int, which is 2; and continue, 3/10, get three; now 432, reverse order;
// boundary case: negative int, 0, -,+ sign discussion +123, out put 123; -123 output -123
// edge case: last char is '\0', think about possible edge case: first char, last char
// Leave blank:
// aplha-numeric equivalent
//string convertToString(int n)
char[] convertToString(int n)
{
//since int maximum size is known, so we can declare an array to store ouput
// char[] output = new char[100];
int [] output = new int[100]; // int 100, may be small one
if(n==0)
return "0";
// more edge case
bool isPositive = true;
if(n<0)
{
n=-n;
isPositive = false; // -100
}
// at least I have one edge case, continue later. need idea?
int count=0;
int loopInt = n;
// go over a test case manually, 324
for(;;)
{
int digitOne = loopInt%10; // first one, 4; 32; 3, digitOne=3
output[count] = digitOne; // output[0]=4, output[1]=2, output[2]=3
count++; // fist time count=1; count =2; count=3
int newLookInt = loopInt/10; // first time, 32; 3;
loopInt = newLookInt; // play safe, late optimize the code // first time 32; 3
if(newLookInt==0) // 32 (1); 3
break; // break the loop, avoid dead loop
}
// output[100] 4, 2, 3 count =3
// cannot use standard libraries, so I have to convert 1 to 1, ascill code? relax the requirement
// since I did some math here using %
/*
string outputStr="";
if(!isPositive)
outputStr = "-";
for(int i=0;i<count;i++)
{
outputStr += output[count-i-1].tostring(); // at least one digit
}
*/
char[] outputStr = new char[100];
int startPos = 0;
if(!isPositive)
{
outputStr[0] = "-";
startPos = 1;
}
for(int i=0;i<count;i++)
{
//outputStr += output[count-i-1].tostring(); // at least one digit
// play safe, I cannot recall the ascill function etc.
int currentIndex = count-i-1;
if(output[currentIndex] == 0)
outputStr[startPos] = '0';
if(output[currentIndex] == 1)
outputStr[startPos] = '1';
if(output[currentIndex] == 2)
outputStr[startPos] = '2';
if(output[currentIndex] == 1)
outputStr[startPos] = '3';
if(output[currentIndex] == 4)
outputStr[startPos] = '4';
if(output[currentIndex] == 5)
outputStr[startPos] = '5';
if(output[currentIndex] == 6)
outputStr[startPos] = '6';
if(output[currentIndex] == 7)
outputStr[startPos] = '7';
if(output[currentIndex] == 8)
outputStr[startPos] = '8';
if(output[currentIndex] == 9)
outputStr[startPos] = '9';
startPos ++;
}
outputStr[startPos]=null; // null terminal string
return outputStr;
// review time, possible problems:
// null terminal string, I am using C#; ? check point 1
// I think my 1 hour time limit is reached. Thanks you for the opportunity to be considered. I like this format
// Wish me good luck
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment