Interview Question for Analysts


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

C uses pass by value.

So mystrcpy gets a copy of str2 (string2), which you overwrite with the result of malloc. This does nothing to str2 of main, which continues to keep its old value, which is NULL.

- Subbu. November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

This will works.
Allocate the memory in main()

#include<stdio.h>
#include<stdlib.h>

void mystrcpy( char *string2 ,char *string1 )
{
while( *string1 != '\0')
{
*string2++= *string1++;

}

}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
str2=malloc(20); //Allocate memory here
mystrcpy( str2,str1);
printf("string1 = %s string2 =%s ",str1,str2) ;
return 0;
}

- SJK November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is good. Make the user of strcpy responsible for allocation. That way you won't be tied to what strcpy uses.

- Anonymous November 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

So here are two different working implementations of your code

#include<stdio.h>
#include<stdlib.h>

void mystrcpy( char **string2 ,char *string1 )
{

*string2 = malloc(20);
char *str = *string2;
while( *string1 != '\0')
{
 *str++ = *string1++;
}

}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
mystrcpy(&str2,str1);
printf("string1 = %s string2 =%s ",str1,str2);
return 0;
}

AND

#include<stdio.h>
#include<stdlib.h>

char* mystrcpy( char *string2 ,char *string1 )
{

string2 = malloc(20);
char *string  = string2;
while( *string1 != '\0')
{
*string2++= *string1++;
}
return string;

}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
str2 = mystrcpy( str2,str1);
printf("string1 = %s string2 =%s ",str1,str2);
return 0;
}

- fReaK November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In second version, why even pass str2?

- Anonymous November 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No need to pass, it was only kept so that the code resembles the original posting.

- fReaK November 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is a simple misunderstanding about Call By Value and Call by Reference...it is call by value (for variables) if you don't mention &..

- findmind February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. put '\0' at the end of string2.
2. Return that string2 .

- pradeep June 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You are incrementing both ptr till it hits '\0' so, it will be priting NULL in both the case ..

To solve the problem.

Dont increment the pointer , access it via array, i.e str1[i] and str2[i] .
Return the base address of str2.

so this should solve your problem.

- cinlinux June 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

malloc() after successful allocation returns (void*).since we are trying to allocate memory to character strings which needs memory to be of (char*). so,typecasting is required to malloc() function by (char*). otherwise compiler can not interpret which type of variable we are allocating hence returns null.

- Madhu July 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

malloc() after successful allocation returns (void*).since we are trying to allocate memory to character strings which needs memory to be of (char*). so,typecasting is required to malloc() function by (char*). otherwise compiler can not interpret which type of variable we are allocating hence returns null.

- Madhu July 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

put '/0' at end you get the string2

- santhosh reddy September 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Correct code

#include<stdio.h>
#include<stdlib.h>
#include<memory.h>

char * mystrcpy( char *string2 ,char *string1 )
{

string2 = (char*)malloc(20);
char * temp = string2;
while( *string1 != '\0')
{
*temp= *string1;
temp++;
string1++;
}

return string2;
}
int main()
{
printf("****************program starts*************************");
char *str1 ="hello world";
char *str2 =NULL;
char *str3 = mystrcpy( str2,str1);
printf("string1 = %s string2 =%s, str3 = %s ",str1,str2,str3) ;
getchar();
return 0;
}

- Anonymous November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

correct code :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char* mystrcpy( char *string2 ,char *string1 )
{
int l,i;
string2 = (char*)malloc(20);
l = strlen(string1);

i=0;
while(i<l)
{
*(string2+i)=*(string1+i);
i++ ;
}
*(string2+i)='\0';
//string2 = "hiii";
return(string2);

}
int main()
{
printf("****************program starts*************************\n");
char *str1 ="hello world";
char *str2 =NULL;
str2 = mystrcpy( str2,str1);
printf("string1 = %s\nstring2 =%s ",str1,str2) ;
return 0;
}

- Zombie December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

because you are incrementing str2 thats why it is going upto last and it is printing NULL here.....so you take another string and then copy the base address of str2 in that siring the print using that string... :)

- ANUJ GARG December 18, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More