0

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.

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • Why not to use `if (s1.find(s2) != std::string::npos) { std::cout << "found!" << '\n'; }`? – CROSP Oct 05 '19 at 06:30
  • In my assignment, it's mentioned to implement a search function by myself without using any standard library functions. – Akhil Kumar Oct 05 '19 at 06:32
  • Asking homework questions is fine, but then you have to mention that you are required to implement that yourself instead of using a library function. – t.niese Oct 05 '19 at 06:34
  • Sorry, I am a newbie here. I will take care of that from next time. – Akhil Kumar Oct 05 '19 at 06:38
  • You can also edit your question to add it now ;) Unrelated, but you should take a look at [Why is using namespace std considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Lukas-T Oct 05 '19 at 06:55
  • What do you mean with "it doesn't work properly at runtime"? Can you give us an example input and describe what happens? – Lukas-T Oct 05 '19 at 07:16

3 Answers3

0

Made some changes in the code worked on my end. Have used const char name; as in Person p("Akhil"); is invalid conversion from 'const char' to 'char*'

#include<iostream>
#include<stdio.h>
#include<string.h>

class Person
{
    const char *name;
public:
    Person(const char* n)
    {
       name=n;
    }
    bool search(char* substr)
    {
        const char *str=name;
        while(*str!='\0')
        {   int count=0;

            if(*str==*substr)
            {   char *s=substr;
                const 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";
}

output i got:

Enter substring to be found:
kh
Akhil
Found!!!

--------------------------------
Process exited with return value 0
Press any key to continue . . .

Software used: Dev-C++ 5.4.2

Compiler used: Compiler: TDM-GCC 4.7.1 64-bit Release

rock123A
  • 82
  • 2
  • 11
  • You removed `using namespace std`, but still write `cout` instead of `std::cout`, so it does not even compile. Also you are using the outdated `gets` with an *uninitilized* buffer `s` -> undefined behaviour. – Lukas-T Oct 05 '19 at 07:34
0

You have to add another condition to the inside while loop.

while(*s != '\0' && *p != '\0')
{
    if(*p == *s)
    {
        count++;
        p++;
        s++;
    }
    else
        break;
}

The reason being you can reach the end of p before reaching the end of s. For example if the string is Akhil and the substring to be found is ill.

srt1104
  • 959
  • 7
  • 11
0

Wild guess: The problem is how you read the string. gets() is deprecated since c++11 and removed as of c++14 as you can read here. You should use std::cin (since you are allow to use std::cout for ouput this should also be allowed?):

std::string s;
std::cin >> s;
if(p.search(s.c_str()))
...

Problem could be, that gets will read input from the console and place it in the buffer you provide. But since you don't initialize your buffer s, you get undefined behaviour.

In addition you should replace char * with const char*, since you are not going to modify any of the strings.

Also avoid using namespace std.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30