K&R C Exercise 2-4 Solution

/*
 * Exercise 2-4 Write an alternate version of squeeze( s1, s2 )
 * that deletes each character in s1 that matches any character
 * in the string s2.
 * 
 * fduan, Dec. 12, 2011.
 */
#include <stdio.h>

void squeeze_char( char s[], int c )
{
	int i, j;
	for( i = j = 0; s[i] != '\0'; ++i )
		if( s[i] != c )
			s[j++] = s[i];
	s[j] = '\0';
}

void squeeze( char s1[], char s2[] )
{
	int i;
	i = 0;
	while( s2[i] != '\0' )
		squeeze_char( s1, s2[i++] );
}

int main()
{
	char s1[] = "abcdefeegba";
	char s2[] = "abc";
	
	printf( "s1 = %s, s2 = %s\n", s1, s2 );

	squeeze( s1, s2 );
	printf( "s1 = %s\n", s1 );

	return 0;
}

你可能感兴趣的:(K&R C Exercise 2-4 Solution)