This is a C Program to remove all characters in second string which are present in first string.
This C Program removes all characters from second string which were present in the first string.
Take input from the user and perform string operations as shown in the program below.
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; }
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.
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.
$ 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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Apply for C Internship
- Check Computer Science Books