Question: The following C function should take two strings, and return characters that occur in both strings, without duplicates, and in the order they appear in

The following C function should take two strings, and return characters that occur in both strings, without duplicates, and in the order they appear in the first string but doesn't work as it should. Are you able to see the problem?
// inter.c
#include
#include
char* my_strchr(char* str, char c)
{
while (++str){
if (*str == c){
return (str);
}
if (*str ==0){
return 0;
}
}
return 0;
}
char *my_strnchr(const char* str, const char c, size_t sz)
{
char *str_tmp =0;
while (sz--){
if (*str == c){
str_tmp =(char *)str;
break;
}
str++;
}
return str_tmp;
}
char* inter(char* str1, char* str2)
{
char* str =(char*)malloc(sizeof(char)*(sizeof(str1)+ sizeof(str2))+1);
size_t sz =0;
char p_str1;
while ((p_str1=*str1++))
{
if(my_strnchr(str, p_str1, sz))
{
continue;
}
if(my_strchr(str2, p_str1))
{
str[sz++]= p_str1;
}
}
return str;
}
int main()
{
char* test;
test = inter("padinton", "paqefwtdjetyiytjneytjoeyjnejeyj"); // "padinto"
// test = inter("ddf6vewg64f", "gtwthgdwthdwfteewhrtag6h4ffdhsd"); // df6ewg4
printf("Result: %s
", test);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!