# Thread: Ex. 2-4 KnR (2nd ed.) loop && array handling

1. ## Ex. 2-4 KnR (2nd ed.) loop && array handling

Heyyy, I'm on exercise 2-4 in k&r 2nd ed.
(Its getting kinda funky but I'm keeping with)

Ex: Write an alternate version of squeeze(s 1, s2) that deletes each character in s1 that matches any character in string s2.

I started off (trying) to copy every non-matching character into a third array. But the loop wasn't working.
So now, I'm trying to fix my error and print a 'Z' insted of deleting.

Code:
```void squeeze1(char s[], char t[]) {
int i, j ;

for (i = 0; s[i] != '\0'; i++) {
for (j = 0; t[j] != '\0'; j++)
if (s[i] == t[j])
s[i] = 'Z';
}
for (i = 0; s[i] != '\0'; i++)
printf("%c", s[i]);
}```
My understanding is that the parent loop initiates, encounters the child loop, completes the child loop and exits (For each element in s, compare to every element in t).

Can anyone show me where I've gone wrong. TIA

2. Code:
```void squeeze2(char s1[], char s2[])
{
int i, j, k;
int instr2 = 0;

for(i = j = 0; s1[i] != '\0'; i++)
{
instr2 = 0;
for(k = 0; s2[k] != '\0' && !instr2; k++)
{
if(s2[k] == s1[i])
{
instr2 = 1;
}
}

if(!instr2)
{
s1[j++] = s1[i];
}
}
s1[j] = '\0';
}```
I got this from a web site with all the answers if you want it I can give it to you but i don't want to temp you by having it here if you want to figure it all out yourself
[EDIT]your code does replace the correct characters with Z's my output
Code:
```Enter 2 arrays
cboard
cprogramming
ZbZZZd```

3. your code does replace the correct characters with Z's my output
I thought it should of done, but it just replaced every single letter, regardless of input. I've tried it again though (after another little rest) and it's working.

In regards to the website, I already check another one out after, or during(if im struggling) an exercise, but I find the code boring and long. Not to mention that the style makes it look like an essay. They also cheat! ( )
Note: It looks like the same site (users.powernet.co.uk)

Thanks for the snippet as well, prompted me to put a little extra error checking in the getchar statements.

Thanks for the help