Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Why this program dosent works? (http://www.go4expert.com/forums/program-dosent-t21565/)

lionaneesh 29Mar2010 17:37

Why this program dosent works?
 
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.

en_7123 30Mar2010 00:02

Re: Why this program dosent works?
 
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


elhassane 30Mar2010 07:52

Re: Why this program dosent works?
 
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 */
   
   
}


shabbir 30Mar2010 09:48

Re: Why this program dosent works?
 
elhassane, the button (http://www.go4expert.com/images/buttons/report.gif) is for reporting spam.

en_7123 30Mar2010 11:25

Re: Why this program dosent works?
 
Quote:

Originally Posted by shabbir (Post 66411)
elhassane, the button (http://www.go4expert.com/images/buttons/report.gif) is for reporting spam.

?????

shabbir 30Mar2010 11:26

Re: Why this program dosent works?
 
Quote:

Originally Posted by en_7123 (Post 66412)
?????

He reported the thread.

en_7123 30Mar2010 11:31

Re: Why this program dosent works?
 
Ok

virxen 30Mar2010 11:55

Re: Why this program dosent works?
 
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;   
}


en_7123 30Mar2010 12:40

Re: Why this program dosent works?
 
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

en_7123 30Mar2010 12:43

Re: Why this program dosent works?
 
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


All times are GMT +5.5. The time now is 13:59.