Design a C program to remove characters from the first string which are present in the second string.
#include <stdio.h>
#include <string.h>
//This function checks if string contains letter
int stringContainsLetter(char intputString[],char letter){
int j=0;
while(intputString[j] != '\0'){
if(intputString[j]==letter){
return 1;
}
j++;
}
return 0;
}
//The start point of the program
int main()
{
char firstString[100];//the first string
char secondString[100];//the second string
char tempFirstString[100];
int i=0;
int counter=0;
int length;
//get the first string from the keyboard
printf("Enter the first string: ");
gets(firstString);
//get the second string from the keyboard
printf("Enter the second string: ");
gets(secondString);
//remove characters from the first string which are present in the second string.
while(firstString[i] != '\0'){
if(stringContainsLetter(secondString,firstString[i])==0){
tempFirstString[counter]=firstString[i];
counter++;
}
i++;Â
}
tempFirstString[counter]='\0';
strcpy(firstString,tempFirstString);
//display the first string after removing characters
printf("The first string after removing characters: %s\n", firstString);
//delay
getchar();
getchar();
return 0;
}
Comments
Leave a comment