Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,883 answers

573 users

How to remove the letters from word1 if they exist in word2 with C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>

void remove_common_letters(const char *word1, const char *word2, char *result) {
    int k = 0;
    for (int i = 0; word1[i] != '\0'; i++) {
        bool found = false;
        for (int j = 0; word2[j] != '\0'; j++) {
            if (word1[i] == word2[j]) {
                found = true;
                break;
            }
        }
        if (!found) {
            result[k++] = word1[i];
        }
    }
    result[k] = '\0';
}

int main() {
    const char *word1 = "forest";
    const char *word2 = "tor";
    char result[64];

    remove_common_letters(word1, word2, result);
    printf("%s\n", result);

    return 0;
}


/*
run:

fes

*/

 



answered Jul 8, 2025 by avibootz
...