Jump to content

[C++] finding substring in string

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Amonijack

Amonijack

    Learning Programmer

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

#2
DrunkenCanadian

DrunkenCanadian

    Newbie

  • Members
  • Pip
  • 3 posts
Here is a quick snippet I wrote to search for substrings :) hope it helps

String Searching

#3
Infinity

Infinity

    Newbie

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

Regards,
Infinity

#4
Amonijack

Amonijack

    Learning Programmer

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

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If you're using std::string class, it has a substr method.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
Amonijack

Amonijack

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
i havent worked with class. I'am interested in simple way. Any code ? Any algorithm maybe ?

#7
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
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.

#8
DrunkenCanadian

DrunkenCanadian

    Newbie

  • Members
  • Pip
  • 3 posts

Amonijack said:

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;

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users