Program to find a substing form a big string.

#include <stdio.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

int main()
{
char sl[50], ss[50], temp[50];
int i, j, longlen, shortlen, instances=0;
char x;

printf("Enter the 2 strings:\n");
scanf("%s %s",sl,ss);
printf("Enter an arbit character: ");
scanf("%c",&x);

/* Assigns the longer string to sl */
if(strlen(ss)>strlen(sl))
{
strcpy(temp,sl);
strcpy(sl,ss);
strcpy(ss,temp);
}

longlen=strlen(sl);
shortlen=strlen(ss);

for(i=0;i<longlen;i++)
{
int flag=TRUE;

for(j=0;j<shortlen;j++)
if(sl[i+j]!=ss[j])
{
flag = FALSE;
break;
}

if(flag==TRUE) instances++;
}

printf("The shorter string occurs %d times in the longer string.\n",instances);

return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s