#include<stdio.h>

void squeez(char s[],char z[]); 

int main()
{

    char s[100] = "Hey wassup !!!!!!";
    char z[100] = "Wow cool!!!!!";
    squeez(s,z);
    return(0);
}

void squeez(char s[] , char z[])
{

    int j,dummy = 0,i=0;
    char x[100];

    for(j = 0 ; s[j] != '\0';j++)/* the starting loop */
    {

        while(z[i] != '\0') /* inner loop */
        {
/           if(s[j] ==z[i])
            {

                dummy = 123;    

                break;
            }

            i++;
        }
        if(dummy != 123)
            {
                x[j] = s[j];
                                                             dummy = 0;
            }
    }

    printf("%s",x); /* printing out the values */
}

The main aim of making this program is that :-

  1. This program/function will delete all those characters in the first string i.e s[] that matches any character in the second string i.e z[]. And copy the new string of characters to x[].

Problems :-

The program is not giving the right output it is printing some garbage value.

Questions:-

Help me with this piece of code . Tell me where i am wrong and what is the solution.

Thank you IN advance.

Recommended Answers

All 4 Replies

#include <stdio.h>
int main(void)
{
    int i=0,j=0,k=0,flag=0;
    char str1[100] = "Hey wassup !!!!!!" , str2[100] = "Wow cool!!!!!", newstr[100];
    while(i!='\0')                          //outer loop start
    {
        while(j!='\0')                      //inner loop start
        {
            if(str1[i]==str2[j])
            {
                flag=1;                     
                break;
            }
            j++;
        }                                   //inner loop end
        if(flag!=1)                         //value 1: char matched
        {
            newstr[k]=str1[i];
            k++;
            flag=0;

        }
         i++;  /* increament*/
    }                                       //outer loop end
    newstr[k]= '\0';
    printf(" %s",newstr);
    return 0;
}

Do not hijack threads with your own questions...

You forgot to reset 'j' to zero in while loop

Odd -- I could have sworn that this problem as asked and solved over a week ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.