Thursday , September 21 2017
Home / C / C program for string matching least time

C program for string matching least time

C program for string matching with best time complexity. It can check/match any string you give in input in any string you have earlier provided as input.

#include<stdio.h>
#include<conio.h>

int length(char x[])
{
    int i;
    for(i=0;x[i]!='\0';i++)
    {}
    return i;
}
void main()
{
	char s[20],p[20];

int i,l,count=0;
clrscr();

printf("\n enter Your String   =  ");
scanf("%s",s);
printf("enter the string to be matched = ");
scanf("%s",p );
l=length(p);

	for(i=0;s[i]!='\0';i++)
	{
	  if(s[i]==p[count] )
		count++;
	else
	{
		count=0;
	}
	  if ( count == l )
		{
			printf("Substring %s found in the given string",p);
		 break;
		}
	}
	if(count!=l)
	printf("not found");

getch();
}

OUTPUT:

string matching

Tags: C program for string matching, Program for string matching in c, best running time program for string matching

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

Leave a Reply

Your email address will not be published. Required fields are marked *