Thursday, 26 January 2017

C Program to find a word is elfish or not using recursion

A word is considered elfish if it contains the letters: e, l, and f in it, in any order.
For example, we would say that the following words are elfish: white leaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive function called elfish?  that, given a word, tells us if that word is elfish or not
Program:

#include <stdio.h>
#include <string.h>
int elfish(char *s,char *b, int i)
{
            if(b[i]=='\0')
                        return 1;
            if(strchr(s,b[i]))
                        return elfish(s,b,i+1);
            else
                        return 0;
}
main()
{
            char s[100],b[4]="elf";
            int result;
            printf("enter the string\n");
            gets(s);
            result=elfish(s,b,0);
            if(result ==0)
                        printf("given string is not elfish");
            else
                        printf("given string is elfish");
}

Output:

 

1 comment: