Microsoft Interview Question
Software Engineer in TestsCountry: United States
Interview Type: Written Test
There are 4 mistakes in this piece of code.
1. while(*a != 0 ) is incorrect, it should be while(*a != '\0')
2. f is incremented in the while loop, so we have lost the reference for the first char of string f.
3. Last char in a char string should be terminating char '\0'.
4. you cant return the reference of a local variable. The life time of this variable is in this method call only.
I think the correct code should be :
void cp(char *a)
{
char *f,*p;
f=(char *)malloc(strlen(a)*sizeof(char));
p = f;
while(*a != '\0') //*a != 0
{
*f=*a;
f++;
a++;
}
*f = '\0';
printf("%s\n",p);
//return f;
}
Please correct me if i am wrong.
// creates a copy of a and also dumps to stdout
char* cp(char *a)
{
char *f;
f=(char*)malloc(strlen(a)*sizeof(char)); // need to allocate an extra character for null termination, malloc may fail, so should initialize f to null, and probably check for it
while(*a != 0)
{
*f=*a; // pointer to beginning of character string is lost, should have a char *g = f before the while and return g instead
f++;
a++;
} // f is not null terminated, should do *f = '\0';
cout<< f;
return f;
}
1 - memory allocated to f should be + 1.
2 - Keep "a" in some temp variable "temp".
3 - just after while loop assign '\0' to *f
4 - assign temp to f. i.e. beginning of the new string.
--------------------------
5 - No error check for "a" - It could be NULL
6 - No error check for "f" after malloc - memory allocation might fail.
class Program
{
public static void Main()
{
string str = "Delhi is Capital of India";
Console.WriteLine("Input");
printaArray(str);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Output");
string[] sarr = str.Split(' ');
int l = sarr.Length;
for (int i = 0; i < l; i++)
{
if (i % 2 == 0)
{
ConvertToCapital(sarr[i]);
}
else
ReverseString(sarr[i]);
}
Console.Read();
}
public static void ConvertToCapital(string str)
{
char[] carr = str.ToCharArray();
for (int i = 0; i < carr.Length; i++)
{
if (carr[i] >= 'a' && carr[i] <= 'z')
carr[i] = (char)((int)carr[i] - 32);
}
printaArray(new string(carr));
}
public static void ReverseString(string str)
{
char[] ch = str.ToCharArray();
int l = ch.Length-1;
char[] temp = new char[l+1];
int i = 0;
while (l >= 0)
{
temp[i++] = ch[l--];
}
printaArray(new string(temp));
}
public static void printaArray(string sarr)
{
Console.Write("{0} ",sarr);
}
}
class Program
{ public static void Main()
{ string str = "Delhi is Capital of India";
Console.WriteLine("Input");
printaArray(str);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Output");
string[] sarr = str.Split(' ');
int l = sarr.Length;
for (int i = 0; i < l; i++)
{ if (i % 2 == 0)
{ ConvertToCapital(sarr[i]); }
else
ReverseString(sarr[i]); }
Console.Read();
}
public static void ConvertToCapital(string str)
{ char[] carr = str.ToCharArray();
for (int i = 0; i < carr.Length; i++)
{ if (carr[i] >= 'a' && carr[i] <= 'z')
carr[i] = (char)((int)carr[i] - 32); }
printaArray(new string(carr)); }
public static void ReverseString(string str) {
char[] ch = str.ToCharArray();
int l = ch.Length-1;
char[] temp = new char[l+1];
int i = 0;
while (l >= 0) {
temp[i++] = ch[l--]; }
printaArray(new string(temp)); }
public static void printaArray(string sarr) {
Console.Write("{0} ",sarr); } }
@Charu:
1) (*a != 0) is equivalent to (*a != '\0'). When you compare an int with a char, the char just gets promoted to an int, as with other integer types.
2) You're right, we should have kept a (fix) reference to the beginning of the memory buffer allocated by malloc.
3) Yes, after the while loop you need the *f = '\0', or *f = 0.
4) That's not totally right. The pointer f (and also p in your modified example) will indeed die after the function returns, but the buffer pointed by it will remain intact on the heap. You should not return a reference or a pointer to neither f nor p, but it's completely safe to return p itself.
Last detail, the malloc should have been as follows:
Otherwise, after you have filled your buffer with strlen(a) bytes inside the while loop, you will be writing your '\0' terminator outside of the allocated memory block.
- Luis March 17, 2015