从字符串s1 中删除所有在s2里出现的字符 squeeze

Write a alternate version of squeeze(s1,s2) that deletes each characters in s1 that maths any character in string s2

 

 

#include 

/*字符串s1 中删除所有在s2里出现的字符*/
void squeeze(char s1[],char s2[])
{

	int i,j,p;
	for (i=0;s2[i];++i)//对S2中每个character,在S1中搜索一边
 	   for (j=0;s1[j];++j)
        	if (s1[j]==s2[i])
			/*如果找到相等的,S1中当前位置的后面character都向前移 
                            动一位,即删除当前相等元素 because 移动了一位,注意
                            当前 j 要相应的向前移动一位*/
			{
                       	for(p=j;s1[p];++p) s1[p]=s1[p+1]; 
				--j;
			}
}

int main()
{
	char s1[]="hello word!";
	char s2[]="lo";
	squeeze(s1,s2);

	printf("%s",s1);
}

 

你可能感兴趣的:(C++/C)