Question
Concat two string with most overlapped substring has to
remove "abcd"+ "cdef" = "abcdef
Question Submitted By :: San
I also faced this Question!!
Rank
Answer Posted By
Re: Concat two string with most overlapped substring has to
remove "abcd"+ "cdef" = "abcdef
Answer
# 1
#include<stdio.h>
main()
{
char *a="abcd";
char *b="cdef";
char c[10];
int i=0;
while(*a != *b)
{
c[i] = *a++;
i++;
}
while(*b != '\0')
{
c[i]= *b++;
i++;
}
printf("%s\n",c);
}
Tarak
Re: Concat two string with most overlapped substring has to
remove "abcd"+ "cdef" = "abcdef
Answer
# 2
above code some thing wrong.
if "abcd","cefg" then output will be "abcdcefg"
but above code will give "abcefg"
Durga Prasad
Re: Concat two string with most overlapped substring has to
remove "abcd"+ "cdef" = "abcdef
Answer
# 3
Check the each char in the second string with first string
if it is not there then concatenate into the first string else
do not do.
Sham
Re: Concat two string with most overlapped substring has to
remove "abcd"+ "cdef" = "abcdef
Answer
# 4
char *strappend1(char *src,char *des)
{
char *tmp=src;
int f=0;
while(*des)
{
while(*src!='\0')
{
if(*src==*des)
{
f=0;
break;
}
else
f=1;
src++;
}
if(f==1)
{
*src++=*des;
*src='\0';
}
des++;
}
return tmp;
}
int main(int argc,char **argv)
{
char *src=argv[1],*des=argv[2];
char *str;
str=strappend1(src,des);
printf("%s",str);
}
Sham
Re: Concat two string with most overlapped substring has to
remove "abcd"+ "cdef" = "abcdef
Answer
# 5
the code given by tarak is correct
ie
#include<stdio.h>
main()
{
char *a="abcd";
char *b="cdef";
char c[10];
int i=0;
while(*a != *b)
{
c[i] = *a++;
i++;
}
while(*b != '\0')
{
c[i]= *b++;
i++;
}
printf("%s\n",c);
}
but the answer is abcdef and some garbage values yar
abcdef{}>>>M<C<P{{
to get perfect answer just add '\o' at end of the code and
before printf dear
#include<stdio.h>
main()
{
char *a="abcd";
char *b="cdef";
char c[10];
int i=0;
while(*a != *b)
{
c[i] = *a++;
i++;
}
while(*b != '\0')
{
c[i]= *b++;
i++;
}
c[i]='\0'; //// new added line here
printf("%s\n",c);
}
Ashwin Kumar
Re: Concat two string with most overlapped substring has to
remove "abcd"+ "cdef" = "abcdef
Answer
# 6
@Ashwin Kumar
According to your program....
char *a="abcdcdcd";
char *b="cdef";
output is "abcdef"..//which is wrong.....it should be abcdcdef
Om