Write menu driven program to perform following operations Write separate function for each option -check if one string is substring of another string -count the number of occurance of character in the string -Exit

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
void substring();
void occurrence();
while(1)
{
int ch;
clrscr();
printf("\n 1:check substring\n");
printf("\n 2:occurrence \n");
printf("\n 3:EXIT");
printf("\n\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:substring();getch();
break;
case 2:occurrence();getch();
break;
case 3:exit(0);
break;
}
}
}
void substring()
{
int s;
char ch[50],sub[10];
printf("\n enter the string : ");
scanf("%s",ch);
printf("\n enter the substring: ");
scanf("%s",sub);
s=substr(ch,sub);
if(s==-1)
printf("\n substring not found");
else
printf("\n substring is found \n");
printf("\n starting location of substring :%d",s);
}

int substr(char ch[50],char sub[10])
{
int b,e,l,i=0,j=0;
//l=strlen(sub);
while(ch[i]!='\0')
{
if(ch[i]==sub[j])
{
b=i;
while(sub[j]!='\0')
{
e=ch[i]-sub[j];
i++;
j++;
}
}
i++;
}
if(e==0)
return(b+1);
else
return(-1);
}

void occurrence()
{
char *s1,*s2,ch;
int cnt;
clrscr();
printf("\n enter the string \n");
scanf("%s",s1);
printf("\n enter the character =");
scanf("%s",s2);
cnt=0;
while(*s1!='\0')
{
if(*s1==*s2)
{
cnt++;
}
s1++;
}
printf("\n %s occour %d times ",s2,cnt);
}

Comments