Home
Questions
Search
Forum
Contact
Guest Book
Polls!
Got a Question?
 
PREV
Programs
NEXT
 
(80 / 301)
 
 



Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?




Here is a simple, yet efficient C program to accomplish the same...


#include <stdio.h>
#include <conio.h>

int isSubset(char *a, char *b);

int main()
{
 char str1[]="defabc";
 char str2[]="abcfed";

 if(isSubset(str1, str2)==0)
 {
   printf("\nYes, characters in B=[%s] are a subset of characters in A=[%s]\n",str2,str1);
 }
 else
 {
   printf("\nNo, characters in B=[%s] are not a subset of characters in A=[%s]\n",str2,str1);
 }

 getch();
 return(0);
}


// Function to check if characters in "b" are a subset
// of the characters in "a"

int isSubset(char *a, char *b)
{
 int letterPresent[256];
 int i;

 for(i=0; i<256; i++)
    letterPresent[i]=0;

 for(i=0; a[i]!='\0'; i++)
    letterPresent[a[i]]++;

 for(i=0; b[i]!='\0'; i++)
    if(!letterPresent[b[i]])
       return(1);

 return(0);
}



PREV
COMMENTS                                  INDEX                                  PRINT
NEXT



Last updated: November 3, 2005

www.cracktheinterview.com - Your destination for the most common IT interview questions, answers, frequently asked interview questions (FAQ), C Programs, C Datastructures for technical interviews conducted by the top IT companies around the world!