用于在较大字符串中查找字符串的函数

时间:2012-06-14 22:33:53

标签: c++ string oop function

以下是我用来实现此程序的完整代码。一切似乎都在编译和运行,但是一旦它运行了我的find方法,程序似乎停止并且不执行指示main.cpp文件中匹配子字符串的最后一行。任何帮助都绝对值得赞赏!

.h文件:

#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文件:

   #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文件:

  #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;
}

4 个答案:

答案 0 :(得分:3)

内循环的不变量似乎是j介于0和end-2之间。因此j永远不会等于结束(“匹配”条件)。

答案 1 :(得分:1)

您的found逻辑似乎有问题。

您的for循环定义为for(int j = 0; j < end -1; j++)

然后你测试if(j == end)

在此for循环中,

j永远不能等于end。考虑一下你在if语句中实际尝试测试的内容。

答案 2 :(得分:0)

我认为你需要在循环之外声明i和j 我认为你的意思是j < end而不是j < end - 1 我认为您需要if((i+j>=end1) || String[i+j] != other.String[j])而不仅仅是if(String[i+j] != other.String[j])

if(j == end)需要在内循环之外。

这是一个类似的实现。

#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);
 }

如果有任何错误或问题,我道歉,没有测试过。

答案 3 :(得分:0)

除了其他人所说的,在你的Substring方法中你有以下代码:

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

花点时间回顾一下while循环的逻辑并问自己“我想在这里实现什么,这个代码实际做了什么?”