Program Discussion :: Strings
342 / 60
Write an efficient program to remove characters from the first string which are present in the second string
Answer:
#include
char *removeChar(char *str, char *mask);
int main()
{
char str[] = "q4interview.com";
char *mask = "iewc";
char *output = removeChar(str, mask);
printf("%s",output);
}
char *removeChar(char *str, char *mask)
{
char arr[256] = {0}, temp;
int i = 0,j=0, k=0;
while (*(mask+i))
{
arr[mask[i]]++;
i++;
}
while (*(str+k))
{
temp = *(str+k);
if (arr[temp] == 0)
{
*(str+j) = *(str+k);
j++;
}
k++;
}
*(str+j)='\0';
return str;
}
Asked In ::
Language:
Ankit
1 Apr, 2018 3:00 PM
#include <stdio.h>
char *removeChar(char *str, char *mask);
int main()
{
char str[] = "q4interview.com";
char *mask = "iewc";
char *output = removeChar(str, mask);
printf("%s",output);
}
char *removeChar(char *str, char *mask)
{
char arr[256] = {0}, temp;
int i = 0,j=0, k=0;
while (*(mask+i))
{
arr[mask[i]]++;
i++;
}
while (*(str+k))
{
temp = *(str+k);
if (arr[temp] == 0)
{
*(str+j) = *(str+k);
j++;
}
k++;
}
*(str+j)='\0';
return str;
}