Why this program dosent works?

lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Code:
#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 */
        {
/* the purpose of dummy is to tell weather this if statement became true or not */
            if(s[j] = z[i])
            {

                dummy = 123;    
        
                break;
            }    
            else
            {
                continue;
            }
            i++;
        }
        if(dummy != 123)
            {
                x[j] = s[j];
            }
    }
    
    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.
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Dude lots of things wrong with that code.I'm posting the updated code this works.I have added comments so you figure it out.
Code:
#include<stdio.h>

void squeez(char *,char *,int ); 

int main()
{

    char s[] = "some text l";
    char z[] = "some text here";
    int l=sizeof(s);    
    squeez(s,z,l);    //call to function
    return(0);
}

void squeez(char *s,char *z,int l)
{
    char *k=z;
        int dummy = 0;
    
    int j=0;
        char x[l];
    
   while(*s !='\0')        /* the starting loop */
    {
        dummy=0;
    k=z;
        while(*k != '\0') /* inner loop */
        {
/* the purpose of dummy is to tell weather this if statement became true or not */
      
          if(*s==*k)
            {

                dummy = 1;    
        
               
            }    //end of if
                
            k++; //increment  pointer
        }        //exit inner loop
        if(dummy ==0)
            {
                x[j] = *s;
        j++;
            }
    s++;
    }  //exit outer loop
    
    puts(x); /* printing out the values */
}  //exit
0
elhassane's Avatar, Join Date: Mar 2010
Newbie Member
Code:
#include<stdio.h>

#include<stdlib.h>
#include<conio.h>
void squeez(char s[],char z[]); 

int main()
{

    char s[100] = "Hey wassup !!!!!!";
    char z[100] = "";
    squeez(s,z);
    system("pause");
    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 */
        {
/* the purpose of dummy is to tell weather this if statement became true or not */
            if(s[j] = z[i])
            {

                dummy = 123;    
        
                break;
            }    
            else
            {
                continue;
            }
            i++;
        }
        if(dummy != 123)
            {
                x[j] = s[j];
            }
    }
    
    printf("%s",x);
    printf("%s","azul tous imazighen"); /* printing out the values */
    
    
}

Last edited by shabbir; 30Mar2010 at 09:42.. Reason: Code blocks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
elhassane, the button () is for reporting spam.
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Quote:
Originally Posted by shabbir View Post
elhassane, the button () is for reporting spam.
?????
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by en_7123 View Post
?????
He reported the thread.
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Ok
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
i think that you must return x[] to then main() function
see my code below.

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

char * squeez(char [],char []);

char * squeez(char s[],char z[]){
    int i,j,found,count=0;
    char *x=NULL;
    for (i=0;s[i];i++){
        found=1;//we suppose that this char-->s[i] is not in second string
        for (j=0;z[j];j++){
            if (s[i]==z[j]){//if this char-->s[i] exists in second string then we supposed wrong!
                found=0;//this char-->s[i] is not good because it exists in second string
                break;//we found a wrong value so stop searching the rest of second string for s[i]
            }
        }
        if (found==1){//yes we found one that not exists in second string
            count++;//increase counter
         x=(char *) realloc(x,count*sizeof(char));  //alocate memory for it
         x[count-1]=s[i]; //store it in x[]
        }

    }
    return x;//return x[] to the main()
}

int main(){
    char first[]="abcdabcdabcdfghfghabcdabcd";//string to test
    char sub[]="abcd";    //string with the chars to delete
    char * x=squeez(first,sub);//call function and get result
    printf("\n    string=<%s>",first);
    printf("\nsub  chars=<%s>",sub);
    printf("\nresult is x[]=<%s>",x);
    getchar();
    return 0;    
}

Last edited by virxen; 30Mar2010 at 12:02..
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Another thing lionannesh following things you need to take care

1 Remember = and == are not the same.In your if statement you use = this would assign the variable a value whereas == checks if two values are equal.
2 When setting a flag dummy try to use 1 for on and 0 for off this makes it easier to understand don't use any arbitrary value
3 When passing arrays try to use pointers.It's much more easier
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Hey shabbir I know this is not relavent to the thread could you take a look at this and help
http://www.go4expert.com/showthread.php?t=21530