I am trying to work on a problem mentioned in K&R book (Excercise 2-4).
Exercise 2-4. Write an alternative version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.
I am not writing a separate function, I am trying to achieve squeeze(s1,s2) by writing the code in the main program itself. Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char msg[15] = "blueberry";
int c;
int s[15];
int j = 0;
int i = 0;
int k= 0;
int ok = 0;
for (i = j = 0; i<=15 && (c = getchar()) != '\0'; ++i)
{
s[i] = c;
ok = 1;
for (k=0; k <= 15; ++k)
{
if (s[i] == msg[k]){
ok = 0;
}
}
if (ok == 1)
{
s[j] = s[i];
j++;
}
}
s[j] = '\0';
for (j = 0; j<= 15; ++j)
printf ("%c ", s[j]);
}
Keyboard input:
Desired output:
My output:
I've done several dry-runs but I cannot figure out where I am going wrong!? Why is control entering highlighted loop even if the condition is false? Extremely baffled! Please help.