Print list items containing all characters of a given word | C ++ program to print all strings that contain all characters of a word ; prints list items having all characters of word ; Since calloc is used , map [ ] is initialized as 0 ; Set the values in map ; Get the length of given word ; Check each item of list if has all characters of word ; unset the bit so that strings like sss not printed ; Set the values in map for next item ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <stdio.h> NEW_LINE #include <string.h> NEW_LINE using namespace std ; # define NO_OF_CHARS  256 NEW_LINE void print ( char list [ ] [ 50 ] , char * word , int list_size ) { int * map = new int [ ( sizeof ( int ) * NO_OF_CHARS ) ] ; int i , j , count , word_size ; for ( i = 0 ; * ( word + i ) ; i ++ ) map [ * ( word + i ) ] = 1 ; word_size = strlen ( word ) ; for ( i = 0 ; i < list_size ; i ++ ) { for ( j = 0 , count = 0 ; * ( list [ i ] + j ) ; j ++ ) { if ( map [ * ( list [ i ] + j ) ] ) { count ++ ; map [ * ( list [ i ] + j ) ] = 0 ; } } if ( count == word_size ) cout << list [ i ] << endl ; for ( j = 0 ; * ( word + j ) ; j ++ ) map [ * ( word + j ) ] = 1 ; } } int main ( ) { char str [ ] = " sun " ; char list [ ] [ 50 ] = { " geeksforgeeks " , " unsorted " , " sunday " , " just " , " sss " } ; print ( list , str , 5 ) ; return 0 ; }