Bloomberg LP Interview Question
Software Engineer / DevelopersOr recursively to avoit the reverse cmd:
void nrOut(int nr){
if(nr) {
nrOut(nr/10);
cout << '0' + (char) nr%10;
}
}
void itoa(int nr) {
if(nr) nrOut(nr) else cout<<"0";
}
}
Sorry, I mean
Or recursively to avoid the reverse cmd:
void nrOut(int nr){
if(nr) {
nrOut(nr/10);
cout << '0' + (char) (nr%10);
}
}
void itoa(int nr) {
if(nr) nrOut(nr) else cout<<"0";
}
My C# code for the same, may be helps someone :)
class itoa
{
string result = string.Empty;
static void Main()
{
itoa sample = new itoa();
int input = -4821;
if (input != 0)
{
sample.itoaRecur(input);
}
else
{
sample.result = "0";
}
Console.WriteLine(sample.result);
}
public void itoaRecur(int input)
{
if (input < 0)
{
result += '-';
input *= -1;
}
if (input > 0)
{
itoaRecur((input / 10));
result += (char)input % 10;
}
}
}
#include<stdio.h>
void reverse(char *s)
{
int i,j;
for(i=0,j=strlen(s)-1;i<j;i++,j--)
{
char t=s[i];
s[i]=s[j];
s[j]=t;
}
}
char* itoa(int n)
{
static char str[50];
int i=0;
while(n)
{
str[i++]=(n%10)+'0';
n=n/10;
}
str[i]='\0';
reverse(str);
return str;
}
main()
{
int n=1234;
char *s;
s=itoa(n);
printf("string is %s\n",s);
return 0;
}
#include <stdio.h>
#include <malloc.h>
int main(){
int n=123;
// Calculate the length of integer
int length=0;
int tmp=n;
do{
tmp=tmp/10;
++length;
}while(tmp!=0);
if (n<0){++length;}
// Convert it into string
char *s = (char*)malloc(sizeof(char)*(length+1));
s[length]='\0';
int i=length-1;
tmp=abs(n);
do{
int rm = tmp%10;
s[i] = rm + '0';
tmp = tmp/10;
--i;
}while(tmp!=0);
if(n<0){s[0]='-';}
printf("%s\n",s);
return 0;
}
Running time O(n)
#include <stdio.h>
#include <malloc.h>
int main(){
int n=123;
// Calculate the length of integer
int length=0;
int tmp=n;
do{
tmp=tmp/10;
++length;
}while(tmp!=0);
if (n<0){++length;}
// Convert it into string
char *s = (char*)malloc(sizeof(char)*(length+1));
s[length]='\0';
int i=length-1;
tmp=abs(n);
do{
int rm = tmp%10;
s[i] = rm + '0';
tmp = tmp/10;
--i;
}while(tmp!=0);
if(n<0){s[0]='-';}
printf("%s\n",s);
return 0;
}
Running time O(n)
int
itoa (int num, char *str, int radix)
{
register int i, neg = 0;
register char *p = str;
register char *q = str;
if (radix == 0) radix = 10;
else if (radix < 2 || radix > RADIX_MAX) {
return (radix);
}
if (num == 0) {
*p++ = '0';
*p = 0;
return (0);
}
if (num < 0) {
neg = 1;
num = -num;
}
while (num > 0) {
i = num % radix;
if (i > 9) i += 7;
*p++ = '0' + i;
num /= radix;
}
if (neg) *p++ = '-';
*p-- = 0;
q = str;
while (p > q) {
i = *q;
*q++ = *p;
*p-- = i;
}
return (0);
}
char *ita(int a)
{//cout<<a;
char c[10],tmp;
int i=0,a1=a,sgn=0;
if (a<0)
{ sgn=1; a*=-1; }
// cout<<a<<endl;
while(a1!=0){
a1/=10;//cout<<a1<<endl;
i++;}
// cout<<i<<endl;
for(a1=0;a1<i;a1++)
{
c[a1]=(a%10) + 48;
a/=10;
} c[i]='\0';
a1=0;i--;
cout<<c<<endl;
while(a1<i)
{
tmp=c[i];
c[i]=c[a1];
c[a1]=tmp;
a1++;
i--;
}
i=strlen(c);
if (sgn==1)
{
for(a1=strlen(c);a1>0;a1--)
{
c[a1]=c[a1-1];
}
c[0]='-';
c[i+1]='\0';
}
cout<<c;
}
This considers value 0 and < 0 too
void myitoa(int int_val, char * str)
{
int abs_val = int_val ;
char tmp_str [32] ;
int str_index = 31 ;
if (int_val == 0 )
{
strcpy(str, "0") ;
return ;
}
abs_val = abs( int_val ) ;
tmp_str[str_index--] = '\0' ;
while(abs_val)
{
int cur_digit ;
cur_digit = abs_val % 10 ;
abs_val /= 10 ;
tmp_str[str_index--] = cur_digit + '0' ;
}
if( int_val <0 )
{
tmp_str [str_index] = '-' ;
strcpy ( str, tmp_str + str_index ) ;
}
else
{
strcpy ( str, tmp_str + str_index + 1 ) ;
}
}
How dumb can you get? This is never a thing interviwer wanted see. You need to implement YOUR 'Itoa' function.
- code_pro March 16, 2009