Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
3.25/5 (4 votes)
See more:
Hey folks,

I have written a function which searches for a given pattern in a given string and
returns yes if the pattern is in the function and no respectively. Plus it also returns the position from where the pattern occurred. e.g., string "hello world" contains pattern "wor" starting from position: 6.

I would be very glad if you could look at my code and provide me with feedback whether it could be more improved or optimized???? Thank you. (ps. the code is working of course, just I am interested in optimization if it is possible).
...
Posted
Updated 30-Oct-12 8:53am
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-12 12:54pm    
First of all, did you test it? Are you sure it works in all imaginable cases?
--SA
[no name] 23-Sep-12 12:57pm    
Yes yes it works as far as I have tested.
Minor comment: the functions length() I mentioned above are specified elsewhere and they also work :).
Sergey Alexandrovich Kryukov 23-Sep-12 14:34pm    
Good. I mean I saw the well-known algorithm (I would need some time to find it again); and it looks like the problem is not as trivial as it might seem. If you can proof that your algorithm is correct, it is implemented in a very compact way, which is good, but performance could be a problem, due to two nested loops.
--SA
[no name] 23-Sep-12 16:55pm    
Dear Sergey thank you for your reply - please see my comment I did to user: "pasztorpisti".
pasztorpisti 23-Sep-12 15:46pm    
Well, either use an existing search implementation, or make the writing of substring serach a bit more challenging for example by making it more efficient for example with KMP:
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
http://www.ics.uci.edu/~eppstein/161/960227.html
There are other bruteforce low-level technical tricks to make your search method faster (like comparing machine-word wide data whenever possible) but playing with algorithms and KMP will be much more fun for you.

You could also check the libraries for standard functions such as strstr()[^].
 
Share this answer
 
Comments
[no name] 23-Sep-12 12:46pm    
thank you for the reference. but I was just interested how "good" was my solution
pasztorpisti 23-Sep-12 13:28pm    
Reinventing small wheels like this is never good.
Sergey Alexandrovich Kryukov 23-Sep-12 19:21pm    
I must disagree, especially about your "never". The history of science and technology is full of examples of re-inventing the small wheels which all thought to be useless but brought very good results. Amazingly, that some of such inventions were about real wheels -- it's enough to look at the modern advanced car or motorcycle wheel to see it.
--SA
pasztorpisti 23-Sep-12 19:25pm    
You are right. In one of my later comments below the question I came to the conclusion that its good that this padavan is searching a solution to find a sequence in another sequence.
Sergey Alexandrovich Kryukov 23-Sep-12 19:58pm    
Great. Thank you for understanding.
--SA
You can go with the mentioned strstr() or if you are using std::string then use std::string::find()[^].

For searching very complex patterns use regular expressions: The 30 Minute Regex Tutorial[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900