Adobe Interview Question
Developer Program EngineersCountry: United States
Can you please explain me the return statements.
It would be better, if you just explain me the entire program, emphasizing the part after while loops are over.
1) what if S2==NULL?
2) what if we consider compare by int instead of byte
3) what if we consider int allignment
4)my code
int mystrcmp(const char* t, const char* s)
{
while((*t)&&(*s)&&((*t++-*s++)==0));
if((*t==0)&&(*s!=0))
return 1;
if((*t!=0)&&(*s==0))
return -1;
else
{
if(*(t-1)>*(s-1))
return 1;
if(*(t-1)<*(s-1))
return -1;
if(*(t-1)==*(s-1))
return 0;
}
}
Sure does, my version below.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char *a;
char *b;
int i;
if (argc !=3)
{
printf ("Incomplete args\n");
exit(-1);
}
a = argv[1];
b = argv[2];
for (i=0;(*a|| *b); i++)
{
if (*a != *b)
{
printf ("Differ at %d, letters are %c, %c\n", i, *a, *b);
exit(-2);
}
a++;
b++;
}
printf("No differences\n");
}
#include<stdio.h>
int strcompare(char *s1,char *s2)
{
int j=0;
int d=0;
while(s1[j]==s2[j]&&(s1[j]!='\0'&&s2[j]!='\0'))
{
printf("%c %c\n",s1[j],s2[j]);
d=1;
j=j+1;
}
if(s1[j]!=s2[j])
{
printf("%c %c\n",s1[j],s2[j]);
d=0;
}
return d;
}
int main(int argc,char **argv)
{
int c=0;
if(argc!=3)
{
printf("Enter two strings for comaprision\n");
return;
}
printf("%s %s %s\n",argv[0],argv[1],argv[2]);
c=strcompare(argv[1],argv[2]);
if(c)
printf("The strings are equal:%d\n",c);
else
printf("The strings are not equal\n");
return 0;
}
Here Its my code.....simplest one.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int c=0;
int strc(int,char *,char *);
int main()
{
char str1[]={"ratnakar pratap singh"};
char str2[]={"ratnakar pretap singh"};
char *s1,*s2;
s1=str1;
s2=str2;
int x=strlen(str1);
int c=strc(x,s1,s2);
if(c==1)
{ printf("strings are equal");
}
else
{ printf("no");}
getch();
return 0;
}
int strc(int x,char *s1,char *s2)
{
while((*s1)!='\0'&&(*s2)!='\0')
{
if(*s1==*s2)
{ c++;
}
s1++;
s2++;
}
if(c==x)
{
return 1;
}
}
does this meet the requirement?
- duckling December 22, 2012