Signup/Sign In
PUBLISHED ON: FEBRUARY 8, 2022

C Program To Remove All Characters In Second String Which Are Present In First String

lean C language tutorial

Logic:

  • Get the input from the user and store it in the variables str & str1,
  • The other variable rem is used to store after removing letters,
  • Here the for loop is used to find the matching letters,
  • If the letter is found it will not print the letters from the str1 to rem variable,

C Language Program To Remove All Characters In Second String Which Are Present In First String:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main ()
{
    char str[50], str1[50], rem[100];
    int x = 0, y = 0, z = 0;
 
    printf ("Enter The Main String:\n");
    fflush (stdin);
    gets (str);
 
    printf ("Enter The Second String To Find:\n");
    gets (str1);
 
    for (x = 0; str[x]!= '\0'; x++)
    {
        for (y = 0; str1[y] != '\0'; y++)
        {
            if (str[x] == str1[y])
            {
                continue;
            }
            else
            {
                rem[z] = str1[y];
                z ++;
            }
        }
        rem[z] = '\0';
        strcpy (str1, rem);
        z = 0;
    }
 
    printf ("Removed The Characters From The Second String: %s\n", rem);
 
    return 0;
}

Output:

Remive



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight