#include <stdio.h>
/* Returns the number of occurrences of string t in s
*
* Example:
* substring_n("hello", "el") => 1
* substring_n("", "ab") => 0
* substring_n("utilities", "ti") => 2
*
*/
int substring_n(char *s, char *t)
{
return -1;
ANS:
#include<stdio.h>
using namespace std;
int substring(char *s,char*t)
{
int j;
int count=0;
int subcount=0;
int index=0;
while(t[index]!='\0')
{
subcount++;
index++;
}
for(int i=0;s[i]!='\0';i++)
{
for( j=0;t[j]!='\0';j++)
{
if(s[i+j]!=t[j])
{
break;
}
}
if(index==j)
{
count++;
j=0;
}
}
return count;
}
int main()
{
char *str1=new char[50];
char *str2=new char[20];
printf(" Enter the first string:");
scanf("%c",str1);
printf(" Enter the first string:\n");
scanf("%c",str2);
printf("%d Count:",substring(str1,str2));
}
Comment down for any queries
Please give a thumbs up if you are satisfied with answer
:)
Get Answers For Free
Most questions answered within 1 hours.