This is my program to find whether a substring exists in a Person's name or not. The implementation of the search function seems correct to me but still, it's not working when I run it in code blocks. There are no compilation errors but it doesn't work properly at runtime.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class Person
{
char *name;
public:
Person(char* n)
{
name=n;
}
bool search(char* substr)
{
char *str=name;
while(*str!='\0')
{ int count=0;
if(*str==*substr)
{ char *s=substr;
char *p=str;
while(*s!='\0')
{
if(*p==*s)
{
count++;
p++;
s++;
}
else
break;
}
}
if(count==strlen(substr))
{
cout<<name<<endl;
return true;
}
str++;
}
return false;
}
void print()
{
cout<<name<<endl;
}
};
int main()
{
Person p("Akhil");
char *s;
cout<<"Enter substring to be found:"<<endl;
gets(s);
if(p.search(s))
cout<<"Found!!!\n";
else
cout<<"Not Found!!!\n";
}
It will be very helpful if someone can tell me where I am making mistake in the implementation of the search function. I can't use a standard library function to implement it as it's mentioned in my assignment.