Puzzle S17

Determine if every character in one string is also in another

[H-5]Write function stringSubset(char *sub, char *set) that returns 1 if every character in the string *sub is also contained in the string *set; otherwise it returns 0. Every char in *sub must also be in *set, but *set may have additional characters. Both strings may have repeated characters. If both strings are empty, or *sub is empty, return 1. If only *set is empty, return 0.

Here is a testing program:

/* Puzzle S17 -- Determine if every character in one string is also in another */
#include <stdio.h>
#include <stdlib.h>

/* return 1 if every character in the string *sub is also contained in the string *set*/
int stringSubset( char const *sub, char const *set)
{

}

int main(int argc, char *argv[])
{
  char *trials[][2] =
  {
    {"a","a"},
    {"a","ab"},
    {"a","aaa"},
    {"ab","ab"},
    {"aaa","a"},
    {"abc","abc"},
    {"a","xyza"},
    {"a","xyzaxyz"},
    {"x","a"},
    {"x","abc"},
    {"ababab","abcxyz"},
    {"aaabbbcccaaa","abc"},
    {"aabbaabbaax","abc"},
    {"xaabbaabbaa","abc"},
    {"aabbxaabbaa","abc"},
    {"aabbxaabbaa","abc"},
    {"aabbxaaxbbaa","abc"},
    {"empty set",""},
    {"","abc"},
    {"",""}
  };

  int j, loc ;
  for ( j=0; j<sizeof(trials)/sizeof(trials[0]); j++ )
  {
    if ( stringSubset( trials[j][0], trials[j][1] ) )
      printf("%s \tcontains only characters from %s\n",
          trials[j][0], trials[j][1] );
    else
      printf("%s \tcontains extra characters than %s\n",
          trials[j][0], trials[j][1] );
  }

  return 0;


Answer         Next Page         Previous Page Home