C Program to Remove Characters in Second String which are present in First String

This is a C Program to remove all characters in second string which are present in first string.

Problem Description

This C Program removes all characters from second string which were present in the first string.

Problem Solution

Take input from the user and perform string operations as shown in the program below.

Program/Source Code

Here is a source code of the C program to remove all characters from the second string that were present in the first string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C 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 str1[100], str2[100], str_rem[100];
    int i = 0, j = 0, k = 0;
 
    printf ("Enter the First string:\n");
    fflush (stdin);
    gets (str1);
 
    printf ("Enter the Second string:\n");
    gets (str2);
 
    for (i = 0; str1[i]!= '\0'; i++)
    {
        for (j = 0; str2[j] != '\0'; j++)
        {
            if (str1[i] == str2[j])
            {
                continue;
            }
            else
            {
                str_rem[k] = str2[j];
                k ++;
            }
        }
        str_rem[k] = '\0';
        strcpy (str2, str_rem);
        k = 0;
    }
 
    printf ("On removing characters from second string we get: %s\n", str_rem);
 
    return 0;
}
Program Explanation

1. In this C program, we are reading two string values using the ‘str1’ and ‘str2’ variables respectively. str1 is used to store the first string and str2 is used to store the second string. str_rem is used to store the remaining string after the character removal.
2. Use fflush() function to get the input from the user. Before getting the input make sure you flush the stdin buffer out.
3. for loop statement is used to check that the match is found or not. If the match is not found, traverse the strings and copy the corresponding characters from str2 to str_rem. If the match is found, just skip copying.
4. Append the \0 null character to the end of str_rem and print the characters removed in the second string which are present in the first string.

advertisement
advertisement
Runtime Test Cases

Test case 1 – Here, the First & Second string are different.

$ gcc removecommonchar.c 
$ ./a.out
 
Enter the First string:
Programming
Enter the Second string:
Computer
 
On removing characters from second string we get: Cpute

Test case 2 – Here, the First & Second string are having some common letters.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ gcc remove-char.c -o remove-char
$ ./remove-char
 
Enter the First string:
sanfoundry
Enter the Second string:
sanppppdry
 
On removing characters from second string we get: pppp

Test case 3 – Here, the First & Second string are same.

$ gcc remove-char.c -o remove-char
$ ./remove-char
 
Enter the First string:
sanfoundry
Enter the Second string:
sanfoundry
 
On removing characters from second string we get:

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.