Showing posts with label C programs. Show all posts
Showing posts with label C programs. Show all posts

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:

 

Wednesday, 4 January 2017

C program to find all substrings of a given string

#include <stdio.h>
#include <string.h>
main()
{
            char name[50];
            int i,j,k,l1;
            gets(name);
            l1=strlen(name);
            for(i=0;i<l1;i++)
            {
                        for(j=0;j<l1;j++)
                        {
                                    for(k=i;k<=j;k++)
                                                printf("%c",name[k]);
                                    printf("\n");
                        }
            }

}