C Program to find the position of a sub string.

C Strings:
C Program to find the position of a sub string in a given string.
In this program, We find the position of a sub string where it starts  in the main string. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>
main()
{
char firststr[30], substr[10];
char *secondstr, *thirdstr;
clrscr();
printf("Enter any string");
scanf("%s",firststr);
printf("Enter sub string");
scanf("%s",substr);
secondstr=firststr;
thirdstr=substr;
i=substr(secondstr, thirdstr);
printf("nn");
if(i!=-1)
printf("The starting location/position of substring in main string: %d", i;
else
printf("String not found");
getch();
}


int substr(secondstr, thirdstr)

{
char *secondstr, *thirdstr;

int j=0,l=0;
static int k;
if(*second==*third)
{
while(i<strlen(third))
{
if(*second==*third)
k=j+1;
else
k+=1;
l++;
}
third++;
second++;
}
second++;
j++;

if(k!=0)
return(k);
else
return(-1);
}
Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply