Hello all, this is my first time on bytes and i was wondering if anyone could give me a hand with my program. I have written a program that, when given an input of 2 words, removes all the letters from the first word that are in the second word. For example, if it is given the words "Hello World", it removes letters from the first word, "Hello" if they occur in the second, "World", leaving the letters "Hel". For some reason, however, it is not 100% accurate. When i enter "Hello World" it outputs "Helo". Often there are one or two letters left in the output that should not be there. I was hoping someone could point out where im going wrong.
Here is the code:
I would really appreciate it if someone could get back to me. Cheers.
Here is the code:
Code:
int main (void){ char x[20]; char y[20]; int i, j; scanf("%[a-z] %[a-z]", x, y); for(i=0; i < strlen(x); i++){ for(j = 0; j < strlen(y); j++){ if(x[i] == y[i]){ x[i] = -1; break; } } } for(i = 0, j = 0; i < strlen(y); i++){ if(x[i] != -1){ x[j] = x[i]; j++; } } x[j] = '\0'; printf("%s", x); getch(); return 0; }
Comment