Slip no : 10_2 WAP for string check if one string is substring of another
count occurences
exit
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void
substring()
{
int
i,j,l2,cnt,count; char str[20],sub[20]; l2=strlen(sub); cnt=0;
printf("\n
enter string : "); scanf("%s",str); // gets(str);
printf("\n
Enter substring : "); scanf("%s",sub);
for(i=0;str[i]!='\0';)
{
j=0;
count=0;
while(str[i]==sub[j])
{
i++;
j++;
count++;
}
if(count==l2)
{
count=0;
}
else
i++;
}
printf("%s is present",sub);
}
void
count()
{
int
i,cnt=0; char str[30]; char ch;
printf("\n
Enter string : "); scanf("%s",str);
printf("\n
Enter character : "); scanf("%c",&ch);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
cnt++;
}
printf("\n %c is present in %s at
%d times",ch,str,cnt);
}
main()
{
char
str[30],s[20]; char ch;
int
choice,ans; clrscr();
do
{
printf("\n
1.Substring \n 2.Occurance of character in string \n 0.exit");
printf("\n Enter your chice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:substring(); break;
case 2:count(); break;
case 0: exit(1);
default : printf("\n Invalid
choice ") ;
}
}
while(choice!=0);
getch();
}
No comments:
Post a Comment