Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C++] finding substring in string

string

  • Please log in to reply
7 replies to this topic

#1 Amonijack

Amonijack

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 89 posts

Posted 11 January 2011 - 05:34 AM

For example : put from keyboard two strings, first amonijack, second codeamonijackcall.
How to check that in codeamonijackcall consists amonijack ?
  • 0

#2 DrunkenCanadian

DrunkenCanadian

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 11 January 2011 - 05:40 AM

Here is a quick snippet I wrote to search for substrings :) hope it helps

String Searching
  • 0

#3 Infinity

Infinity

    CC Newcomer

  • Validating
  • PipPip
  • 24 posts

Posted 11 January 2011 - 06:35 AM

Hello, you can download some regex library like this: Boost C++ Libraries. Learn something about it, its very useful.

Regards,
Infinity
  • 0

#4 Amonijack

Amonijack

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 89 posts

Posted 11 January 2011 - 06:53 AM

every one suggest that, for my problem some objectly advice ?
what to do when extract that ? :S
  • 0

#5 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 11 January 2011 - 12:51 PM

If you're using std::string class, it has a substr method.
  • 0

The roots of education are bitter, but the fruit is sweet.


#6 Amonijack

Amonijack

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 89 posts

Posted 11 January 2011 - 03:10 PM

i havent worked with class. I'am interested in simple way. Any code ? Any algorithm maybe ?
  • 0

#7 Skippy

Skippy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 134 posts

Posted 11 January 2011 - 03:28 PM

You are learning c++ but you haven't learned "class"? You mean you are learning c.

One of the best quotes I've heard is: "the best programmers are lazy".

Meaning: why reinvent the wheel if you don't have to? you don't have to spend resources, time, and money trying to invent something that's already been done.

If you were assigned this as a problem for a class where you are supposed to design a function that does what you mentioned, then that's a different story, and please say so.

If you are just needing to parse a string and search for an occurrence of another string within it, then please consider using tools which have already been designed to suit that need such as the std::string class and the boost library like people have mentioned above.
  • 0

#8 DrunkenCanadian

DrunkenCanadian

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 11 January 2011 - 06:59 PM

i havent worked with class. I'am interested in simple way. Any code ? Any algorithm maybe ?


Did you even look at the link I posted? I suppose not, so here is the code I wrote...

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	if (argc > 2)
	{
		char* Phrase = argv[1];
		char* SearchString = argv[2];
		int found = 0;

		// loop through the phrase
		for (int i = 0; i < strlen(Phrase); i++)
		{
			// did we find a char that is the beginning of our substring?
			if (Phrase[i] == SearchString[0])
			{
				// lest see if all of the characters match our substring.
				for (int k = 0; k < strlen(SearchString); k++)
				{
					// loop through the phrase by our substring length.
					if (Phrase[i+k] == SearchString[k])
					{
						found++;
						if (found == strlen(SearchString))
							cout << "\"" << argv[2] << "\" is a substring of \"" << argv[1] << "\"" << endl;
					}
				}
			}
		}

		if (found != strlen(SearchString))
			cout << "\"" << argv[2] << "\"is not a substring of \"" << argv[1] << "\"" << endl;
	}

	else
		cout << "The correct usage is: \"phrase\" \"substring\"";

	return 0;
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download