Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:21:55+00:00 2026-06-05T16:21:55+00:00

Here are the full codes that I am using to implement this program. Everything

  • 0

Here are the full codes that I am using to implement this program. Everything seems to compile and run, but once it runs my find method, the program seems to stop and does not execute the last line stating the matching substring within the main.cpp file. Any help is definitely appreciated!

.h file:

#include <iostream>
using namespace std;

class MyString
{
    public:
            MyString();
            MyString(const char *message);
            MyString(const MyString &source);
            ~MyString();
            const void Print() const;
            const int Length() const;
            MyString& operator()(const int index, const char b);
            char& operator()(const int i);

            MyString& operator=(const MyString& rhs);
            bool operator==(const MyString& other) const;
            bool operator!=(const MyString& other) const;
            const MyString operator+(const MyString& rhs) const;
            MyString& operator+=(const MyString& rhs);
            friend ostream& operator<<(ostream& output, const MyString& rhs);
            const int Find(const MyString& other);
            MyString Substring(int start, int length);

    private:
            char *String;
            int Size;


   };

  istream& operator>>(istream& input, MyString& rhs);

.cpp file:

   #include <iostream>
  #include <cstdlib>
  #include "MyString.h"

  using namespace std;

  //default constructor that sets the initial string to the value "Hello World"
 MyString::MyString()
 {
    char temp[] = "Hello World";

    int counter(0);
    while(temp[counter] != '\0')
    {
            counter++;
    }
    Size = counter;
    String = new char [Size];
    for(int i=0; i < Size; i++)
            String[i] = temp[i];

 }

 //alternate constructor that allows for setting of the inital value of the string
 MyString::MyString(const char *message)
{
    int counter(0);
    while(message[counter] != '\0')
    {
            counter++;
    }
    Size = counter;
    String = new char [Size];
    for(int i=0; i < Size; i++)
            String[i] = message[i];
  }

    //copy constructor
  MyString::MyString(const MyString &source)
  {

     int counter(0);
    while(source.String[counter] != '\0')
    {
            counter++;
    }
    Size = counter;
    String = new char[Size];
    for(int i = 0; i < Size; i++)
            String[i] = source.String[i];
  }

 //Deconstructor
 MyString::~MyString()
{
    delete [] String;
 }

//Length() method that reports the length of the string
const int MyString::Length() const
{
    int counter(0);

    while(String[counter] != '\0')
    {
            counter ++;
    }
    return (counter);
 }

/*Parenthesis operator should be overloaded to replace the Set and Get functions of    your previous assignment. Note that both instances should issue exit(1) upon violation of the string array bounaries.
*/
 MyString& MyString::operator()(const int index, const char b)
{
    if(String[index] == '\0')
    {
            exit(1);
    }
    else
    {
            String[index] = b;
    }
}

char& MyString::operator()(const int i)
{
    if(String[i] == '\0')
    {
            exit(1);
    }
    else
    {
            return String[i];
    }
}
 /*Assignment operator (=) which will copy the source string into the destination string. Note that size of the destination needs to be adjusted to be the same as the source.
 */
   MyString& MyString::operator=(const MyString& rhs)
  {
    if(this != &rhs)
    {
            delete [] String;
            String = new char[rhs.Size];
            Size = rhs.Size;

            for(int i = 0; i < rhs.Size+1 ; i++)
            {
                    String[i] = rhs.String[i];
            }
    }

    return *this;
  }
 /*Logical comparison operator (==) that returns true iff the two strings are identical in size and contents.
 */
  bool MyString::operator==(const MyString& other)const
  {
    if(other.Size == this->Size)
    {
            for(int i = 0; i < this->Size+1; i++)
            {
                    if(&other == this)                               

                        return true;
            }
    }
    else
            return false;
    }

 //Negated logical comparison operator (!=) that returns boolean negation of 2
    bool MyString::operator!=(const MyString& other) const
  {
    return !(*this == other);
  }

 //Addition operator (+) that concatenates two strings
 const MyString MyString::operator+(const MyString& rhs) const
 {
    char* tmp = new char[Size + rhs.Size +1];

    for(int i = 0; i < Size; i++)
    {
            tmp[i] = String[i];
    }
    for(int i = 0; i < rhs.Size+1; i++)
    {
            tmp[i+Size] = rhs.String[i];
    }
    MyString result;

    delete [] result.String;
    result.String = tmp;
    result.Size = Size+rhs.Size;

    return result;
 }
 /*Addition/Assigment operator (+=) used in the following fashion: String1 += String2 to operate as String1 = String1 + String2
*/
MyString& MyString::operator+=(const MyString& rhs)
{
    char* tmp = new char[Size + rhs.Size + 1];

    for(int i = 0; i < Size; i++)
    {
            tmp[i] = String[i];
    }        for(int i = 0; i < rhs.Size+1; i++)
    {
            tmp[i+Size] = rhs.String[i];
    }

    delete [] String;
    String = tmp;
    Size += rhs.Size;

    return *this;
 }
 istream& operator>>(istream& input, MyString& rhs)
{
    char* t;
    int size(256);
    t = new char[size];
    input.getline(t,size);

    rhs = MyString(t);
    delete [] t;

    return input;
 }

 ostream& operator<<(ostream& output, const MyString& rhs)
{
    if(rhs.String != '\0')
    {
            output << rhs.String;
    }
    else
    {
            output<<"No String to output\n";
    }

    return output;
 }

  const int MyString::Find(const MyString& other)
 {
       int nfound = -1;

    if(other.Size > Size)
    {
            return nfound;
    }
    int i = 0, j = 0;
    for(i = 0; i < Size; i++)
    {
            for(j = 0; j < other.Size; j++)
            {
                    if( ((i+j) >= Size) || (String[i+j] != other.String[j]) )
                    {
                            break;
                    }

            }

            if(j == other.Size)
            {
                    return i;
            }

    }

    return nfound;
    }
  /*MyString::Substring(start, length). This method returns a substring of the original string that contains the same characters as the original string starting at location start and is as long as length.
 */

 MyString MyString::Substring(int start, int length)
 {
    char* sub;
    sub = new char[length+1];


    while(start != '\0')
    {
            for(int i = start; i < length+1; i++)
            {
                    sub[i] = String[i];
            }
    }
    return MyString(sub);
 }

 //Print() method that prints the string
 const void MyString::Print() const
 {

    for(int i=0; i < Size; i++)
    {
            cout<<String[i];
    }
    cout<<endl;
  }

main.cpp file:

  #include <cstdlib>
  #include <iostream>
 #include "MyString.h"


  using namespace std;

 /*
 *
 */
int main (int argc, char **argv)
{

 MyString String1; // String1 must be defined within the scope

 const MyString ConstString("Target string");          //Test of alternate constructor

  MyString SearchString;  //Test of default constructor that should set "Hello World". W/o ()
 MyString TargetString (String1); //Test of copy constructor


  cout << "Please enter two strings. ";
 cout << "Each string needs to be shorter than 256 characters or terminated by /\n." << endl;
 cout << "The first string will be searched to see whether it contains exactly the second string. " << endl;

  cin >> SearchString >> TargetString; // Test of cascaded string-extraction operator


 if(SearchString.Find(TargetString) == -1) {
      cout << TargetString << " is not in " << SearchString << endl;
   }
   else {
    cout << TargetString << " is in " << SearchString << endl;
    cout << "Details of the hit: " << endl;
    cout << "Starting poisition of the hit: " << SearchString.Find(TargetString) << endl;
    cout << "The matching substring is: " << SearchString.Substring(SearchString.Find(TargetString), TargetString.Length());
  }
 return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:21:57+00:00Added an answer on June 5, 2026 at 4:21 pm

    I think you need to declare i and j outside the loops.
    I think you meant j < end and not j < end - 1
    I think you need to if((i+j>=end1) || String[i+j] != other.String[j]) and not just if(String[i+j] != other.String[j])

    and if(j == end) needs to be outside the inner loop.

    Here is a similar implementation.

    #include <string>
    #include <iostream>
    using namespace std;
    
    class MyString
    {
        private:
            string String;
            unsigned int Size;
        public:
            MyString() {
                this->String = "";
                this->Size = 0;                    
            }
            MyString(string initial_value) {
                this->String = initial_value;
                this->Size = initial_value.length();
            }        
    
            const int Find(const MyString& other);       
    };
    
    const int MyString::Find(const MyString& other)
    {                  
        if (other.Size > Size)
            return -1; // if the substring is greater then us, there's no way we can have it as a substring
        int i = 0, j = 0;
        for (i = 0; i < Size; i++)
        {
            for (j = 0; j < other.Size; j++)  
                if ( ((i + j) >= Size) || (String[i + j] != other.String[j]) ) // if they don't match, offset exceeded Size, break
                    break ;
            if (j == other.Size) // We went through the entire substring, didn't hit break so j == Other.size
                return i; // return index
        }
    
        return -1; // if we never return anything means, we didn't find it, so return -1
    }
    
    int main()
    {
        string temp1, temp2;
        getline(std::cin, temp1, '\n');
        getline(std::cin, temp2, '\n');
        MyString main_string(temp1), sub_string(temp2);
    
        cout << main_string.Find(sub_string) << endl;
        return 0;
    }
    
    
    MyString MyString::Substring(int start, int length)
    {
        char* sub = new char[length + 2]; // 2 byte buffer to be safe
    
        int i = 0;
        for (i = 0; i < length; i++)           
            sub[i] = String[start + i]; 
        sub[i] = '\0'; // always null terminated to be safe!           
    
        return MyString(sub);
     }
    

    if theres any bugs or issues, I apologize, haven’t tested it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my full, latest code that doesn't work. Here's the main window HTML <!DOCTYPE
Here's some code (full program follows later in the question): template <typename T> T
See the full error here: http://notesapp.heroku.com/ I'm using DataMapper and dm-validations 0.10.2. No matter
Why does an?|the not work? I'm using PHP's preg_match(). Here's the full regex: /^an?|the
Is it possible to implement a function that results in this mapping: { (0x01,
I'm using Visual c++. I'm trying to implement a circular buffer, this CB must
I'm trying to implement zooming functionality using a UIScrollView in MonoTouch but I just
I'm trying to implement dynamic array in C using realloc(). My understanding is that
When using the windows SDK to compile a program writen in C++, if I
you can view full source code here dpaste.com/hold/167199 Error: delete() takes exactly 2 arguments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.