Help with this program please !!

Scorpy

Adept
Well I wrote this program which checks if a string is present in another string

example : say 1st string is "COMPUTER"
2nd string is "PUT"

then basically its present .... I wrote the following program but Im getting epic fail ... D: ... please help me spot my mistake <.> .

Code:
#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

int check(string s1,string s2)
{
	int c=0;
	int x=s1.length();
	for(int i=0;i<20;i++)
	{
		if(s1[i]==s2[0])
		{
			c=i;
		   for(int j=0;j<x;j++)
		   {
			   if(s1[c]!=s2[j])
			   {
				   return 0;
			   }
		   c++;
		   }
		}
	}
return 1;
}
				 

void main()
{
	string s1="";
	string s2="";
    int x=0;
	cout<<"Enter 1st string";
	getline(cin,s1);
	cout<<"Enter 2nd string";
	getline(cin,s2);
	x=check(s1,s2);

	if(x==1)
	{
		cout<<"OMG WOOT !!!";
	}
	else 
    cout<<"EPIC FAIL !!!";

}
 
akhurana said:
for(int i=0;i<20;i++)

shuldn't it be
i<s1.length()

and instead of
for(int j=0;j<x;j++)

j<s2.length();

Actually.. it should be i<(strlen(s1) - strlen(s2))...
And instead of return 0 in your program, you should do a break; instead.. because otherwise your code won't detect "pet" in "puppet" .. first failure will terminate the function :S

and try pasting the code you actually wrote in your compiler.. it should be #include<string.h> .. ;)

Cheers! :hap2:
 
Back
Top