Hello this is my first post here, i made this search function and i would like some feedback and or tips. (:

// Implement your own [search]strstr[/search] function. (Intermediate)

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Search
{
private:
	int m_nTextL;
	int m_nSearchL;
	int m_nResult;
	bool m_bFound;

public:
	Search(string strText = 0, string strSearch = 0)
		: m_nTextL(strText.length()-1)
		, m_nSearchL(strSearch.length()-1)
		, m_nResult(0), m_bFound(false)
	{
		int aaa = 0; int bbb = 0;

		while(1)
		{
			while(strSearch[aaa] == strText[bbb++])
				{ aaa++; break; }

			if(aaa>0 && !(strSearch[aaa] == strText[bbb])) aaa = 0;

			if(aaa == m_nSearchL)
			{ m_bFound = true; m_nResult = bbb--; break; }

			if(bbb == m_nTextL)
			{ m_bFound = false; m_nResult = 0; break; }
		}

		if(m_bFound == true)	for(bbb--; bbb<=m_nTextL; bbb++)
						cout << strText[bbb];

		else cout << "No match!";
		cout << endl;
	}
};

int main()
{
	Search jeg("This is the text i want to search in", "want");
	system("pause");
	return 0;
}
Nick Evan commented: Indented code + code-tags on the first post. Good job! +12
Re: My str search function 80 80

I wouldn't do everything in the constructor. I would make it more like this:

Search mySearch;
mySearch.SetText("This is the text I want to search in");
mySearch.SetSearchString("want");
bool found = mySearch.Search();

This way you also can use the result in the calling function.

Dave

Re: My str search function 80 80

Use cin.get() instead of system("pause"). It is slow and non-portable. Read this thread for more info http://www.daniweb.com/forums/post58481.html#post58481

Does this code work?

Re: My str search function 80 80

Its pointless doing this since, you are using string, unless you are doing this as an
exercise.

string text = "This is the text I want to search in";
string wordToFind = "want";
bool isFound = text.find(wordToFind) != string::npos;
Re: My str search function 80 80

Use cin.get() instead of system("pause"). It is slow and non-portable. Read this thread for more info http://www.daniweb.com/forums/post58481.html#post58481

Does this code work?

Yea it works. Just an exercise tho

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.