Find sub string position in a string

Here is a very short program for finding a sub siting in string
Hope you like it :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{int i,k,flag=0,pos=0;
char str[30],sub[20];
cout<<"Enter a string :\n";
gets(str);
cout<<"\nEnter the sub string to be searched :\n";
gets(sub);
for(i=0;i<strlen(str);i++)
{	for(k=0;k<strlen(sub);k++)
		{
		if(str[i]==sub[k])
			{flag=1;
			pos=i+1;
			continue;}
		 else
		        {flag=0;
                         break;}
		 }
}
if(flag==1)
cout<<"\nThe sub string \""<<sub<<"\" is at position "<<pos;
else
cout<<"\nThe string is not present!";
getch();
}
Topic archived. No new replies allowed.