Why this program dosent works?

Discussion in 'C' started by lionaneesh, Mar 29, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    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.
     
  2. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    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
    
     
  3. elhassane

    elhassane New Member

    Joined:
    Mar 30, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    ouarzazate
    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 a moderator: Mar 30, 2010
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    elhassane, the button ([​IMG]) is for reporting spam.
     
    Last edited: Jan 21, 2017
  5. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    ?????
     
    Last edited by a moderator: Jan 21, 2017
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    He reported the thread.
     
  7. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
  8. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    i think that you must return x[] to then main() function
    see my code below.

    Code:
    [COLOR="SeaGreen"]#include <stdio.h>
    #include <stdlib.h>[/COLOR]
    
    [COLOR="Blue"]char[/COLOR] * squeez([COLOR="Blue"]char[/COLOR] [],[COLOR="Blue"]char[/COLOR] []);
    
    [COLOR="Blue"]char[/COLOR] * squeez([COLOR="Blue"]char[/COLOR] s[],[COLOR="Blue"]char[/COLOR] z[]){
        [COLOR="Blue"]int[/COLOR] i,j,found,count=0;
        [COLOR="Blue"]char[/COLOR] *x=[COLOR="Blue"]NULL[/COLOR];
        [COLOR="Blue"]for[/COLOR] (i=0;s[i];i++){
            found=1;[COLOR="SeaGreen"]//we suppose that this char-->s[i] is not in second string[/COLOR]
            [COLOR="Blue"]for[/COLOR] (j=0;z[j];j++){
                [COLOR="Blue"]if[/COLOR] (s[i]==z[j]){[COLOR="SeaGreen"]//if this char-->s[i] exists in second string then we supposed wrong![/COLOR]
                    found=0;[COLOR="SeaGreen"]//this char-->s[i] is not good because it exists in second string[/COLOR]
                   [COLOR="Blue"] break[/COLOR];[COLOR="SeaGreen"]//we found a wrong value so stop searching the rest of second string for s[i][/COLOR]
                }
            }
            [COLOR="Blue"]if[/COLOR] (found==1){[COLOR="SeaGreen"]//yes we found one that not exists in second string[/COLOR]
                count++;[COLOR="SeaGreen"]//increase counter[/COLOR]
             x=([COLOR="Blue"]char[/COLOR] *) [COLOR="Blue"]realloc[/COLOR](x,count*[COLOR="Blue"]sizeof[/COLOR]([COLOR="Blue"]char[/COLOR]));  [COLOR="SeaGreen"]//alocate memory for it[/COLOR]
             x[count-1]=s[i]; [COLOR="SeaGreen"]//store it in x[][/COLOR]
            }
    
        }
       [COLOR="Blue"] return[/COLOR] x;[COLOR="SeaGreen"]//return x[] to the main()[/COLOR]
    }
    
    [COLOR="Blue"]int [/COLOR]main(){
        [COLOR="Blue"]char[/COLOR] first[]="abcdabcdabcdfghfghabcdabcd";[COLOR="SeaGreen"]//string to test[/COLOR]
        [COLOR="Blue"]char[/COLOR] sub[]="abcd";    [COLOR="SeaGreen"]//string with the chars to delete[/COLOR]
        [COLOR="Blue"]char[/COLOR] * x=squeez(first,sub);[COLOR="SeaGreen"]//call function and get result[/COLOR]
        [COLOR="Blue"]printf[/COLOR]("\n    string=<%s>",first);
        [COLOR="Blue"]printf[/COLOR]("\nsub  chars=<%s>",sub);
        [COLOR="Blue"]printf[/COLOR]("\nresult is x[]=<%s>",x);
        [COLOR="Blue"]getchar[/COLOR]();
        [COLOR="Blue"]return[/COLOR] 0;    
    }
    
     
    Last edited: Mar 30, 2010
  9. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  10. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have very little idea about that and instead of posting things here you can always leave me a message on my profile.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice