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 do not exist in word2 with C

1 Answer

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

void remove_non_common_letters(const char* word1, const char* word2, char* result) {
    int idx = 0;
    
    for (int i = 0; word1[i] != '\0'; ++i) {
        if (strchr(word2, word1[i])) {
            result[idx++] = word1[i];
        }
    }
    
    result[idx] = '\0'; // Null-terminate the result string
}

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

    remove_non_common_letters(word1, word2, result);

    printf("%s\n", result);

    return 0;
}



/*
run:

ort

*/

 



answered Jul 9, 2025 by avibootz
...