Web Development for 2012

Alexander Dickson - Web Developer covering PHP, jQuery / Javascript, XHTML, CSS, more

Remove characters from a C string which occur in a second string

Published on Saturday, 21st August 2010.

This morning's 2AM project is K&R's Exercise 2-4. It takes two arguments, the first, a source string, and the second, a string of characters which should be removed from the first if present.

void squeeze(char str1[], const char str2[]) { int i = 0; int y = 0;   while (str1[i] != '\0') { int j = 0; int found = 0; while (str2[j] != '\0') { if (str1[i] == str2[j]) { found = 1; break; } ++j; } if ( ! found) { str1[y++] = str1[i]; } ++i; } // Trim off any excess string str1[i - (i - y)] = '\0'; }
Write an alternate version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.

Comments

No comments yet.

Leave a Comment

Comment Details

Your email will never be displayed. If you have a gravatar, it will be displayed.

Note: Your comment may require approval before it is posted to the site.

Stack Overflow Profile

view full profile »

About

I'm a web developer from the Sunshine Coast, Australia. more »