Thanks for your answers
@zacs
this is not homework. I found the exercise(as many others) in a site and i am trying to do it for practise only
i will try your tips and if i have a problem i will post again.So see you soon in a few hours
Thanks for your answers
@zacs
this is not homework. I found the exercise(as many others) in a site and i am trying to do it for practise only
i will try your tips and if i have a problem i will post again.So see you soon in a few hours
mistake
Last edited by alzar; 09-15-2007 at 05:17 AM.
i made another try.Still i don't use malloc but an array.If this code work after i write it with malloc.The problem is that it doesn't print me nothing
where is the mistake?Code:#include <stdio.h> #include <string.h> char ar[20]; void fun(char *s1, char *s2); int main(void) { char *p1="hellosw"; char *p2="ahellogw"; fun(p1,p2); printf(ar); return 0; } void fun(char *s1, char *s2) { int i,j,l; int found; int slen1,slen2; slen1=strlen(s1); slen2=strlen(s2); found=0; l=0; for(i=0; i<slen1; i++) { for(j=0; j<slen2; j++) { if(s1[i]==s2[j]) /*for each element of s1[i] i check if there is also in s2*/ found=1; } if (found=0) ar[l++]=s1[i]; /*if not i copy the character to array ar*/ } }
useor justCode:if (found==0)
KurtCode:if (!found)
still doesn't print anything with found==0
ssharishCode:for(i=0; i<slen1; i++) { found = 0; for(j=0; j<slen2; j++) { if(s1[i]==s2[j]) { found=1; break;} } if (found==0) ar[l++]=s1[i]; }
yes now prints me the hole first string and i don't want that
i think that in the loops i am making these:
if s1="het" and s2="aht"
for i=0
for j=0
s1[0]=s2[0] (h=a) (false)
for j=1
s1[0]=s2[1] (h=h) (true and break)
for i=1
for j=0
s1[1]=s2[0] (e=a) (false)
for j=1
s1[1]=s2[1] (e=h) (false)
for j=2
s1[1]=s2[2] (e=t) (false)
then
found is still 0
I am putting e in ar[0]
etc.
Does my code do these things that i am describing to you or not?
If yes then why don' t print what i want?
i think you are looking for this
ssharishCode:for(i=0; i<slen2; i++) { found = 0; for(j=0; j<slen1; j++) { if(s1[j]==s2[i]) /*for each element of s1[i] i check if there is also in s2*/ { found=1; break;} } if (found==0) { ar[l++]=s2[i]; /*if not i copy the character to array ar*/ }
You have to reset found to 0 inside the outher loop.
Kurt
@ssharish
i want the characters from first string to copy to ar.BUT these characters that i copy should not be in second string(s2)
example
s1= hello
s2= hallo
then ar has only e because h,l and o are also in s2
so with your code ar[l++]=s2[i];
i don't think that does what i want
@kurt
what do you mean? this?
because i make it and don't work as i wantCode:for(i=0; i<slen1; i++) { found = 0; for(j=0; j<slen2; j++) if(s1[i]==s2[j])
Last edited by alzar; 09-17-2007 at 02:18 PM.
You get the output. What else u need?Code:#include <stdio.h> #include <string.h> char ar[20]; void fun(char *s1, char *s2); int main(void) { char *p1="hello"; char *p2="hallo"; fun(p1,p2); printf(ar); getchar(); return 0; } void fun(char *s1, char *s2) { int i,j,l; int found; int slen1,slen2; slen1=strlen(s1); slen2=strlen(s2); found=0; l=0; for(i=0; i<slen1; i++) { found = 0; for(j=0; j<slen2; j++) { if(s1[i]==s2[j]) /*for each element of s1[i] i check if there is also in s2*/ { found=1; break;} } if (found==0) { ar[l++]=s1[i]; /*if not i copy the character to array ar*/ } } } /* my output e */
ssharish
YES why do you use getchar??
In my code i don't use getchar.
Can you explain me?
With getchar the code runs as i want but why?
Because i need to Stop my Windows Console from disappearing everytime I run my program. Since I am using Dev-C++.
You could ignore that, if you want. If you are on console.
ssharish
Thanks i found the problem .It works also without getchar