<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>text processing software &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/text-processing-software/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Brute Force String Matching</title>
		<link>/2012/03/27/computer-algorithms-brute-force-string-matching/</link>
		<comments>/2012/03/27/computer-algorithms-brute-force-string-matching/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 07:21:41 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Bitap algorithm]]></category>
		<category><![CDATA[Boyer–Moore string search algorithm]]></category>
		<category><![CDATA[brute force algorithms]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[pre-processing]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[sequential search]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[String algorithms]]></category>
		<category><![CDATA[string matching algorithm]]></category>
		<category><![CDATA[String searching algorithm]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[text processing software]]></category>

		<guid isPermaLink="false">/?p=2966</guid>
		<description><![CDATA[Introduction String matching is something crucial for database development and text processing software. Fortunately every modern programming language and library is full of functions for string processing that help us in our everyday work. However is great to understand their principles. String algorithms can be mainly divided into several categories. One of these categories is &#8230; <a href="/2012/03/27/computer-algorithms-brute-force-string-matching/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Brute Force String Matching</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/04/02/computer-algorithms-rabin-karp-string-searching/" rel="bookmark" title="Computer Algorithms: Rabin-Karp String Searching">Computer Algorithms: Rabin-Karp String Searching </a></li>
<li><a href="/2012/04/09/computer-algorithms-morris-pratt-string-searching/" rel="bookmark" title="Computer Algorithms: Morris-Pratt String Searching">Computer Algorithms: Morris-Pratt String Searching </a></li>
<li><a href="/2012/04/17/computer-algorithms-boyer-moore-string-search-and-matching/" rel="bookmark" title="Computer Algorithms: Boyer-Moore String Searching">Computer Algorithms: Boyer-Moore String Searching </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>String matching is something crucial for database development and text processing software. Fortunately every modern programming language and library is full of functions for string processing that help us in our everyday work. However is great to understand their principles.</p>
<p>String algorithms can be mainly divided into several categories. One of these categories is string matching.</p>
<p>When we come to string matching the most basic approach is what is known as brute force, which means just to check every single character from the text to match against the pattern. In general we have a text and a pattern (most commonly shorter than the text). What we need to do is to answer the question whether this pattern appears into the text.</p>
<h2>Overview</h2>
<p>The principles of brute force string matching are quite simple. We must check for a match between the first characters of the pattern with the first character of the text as on the picture bellow.</p>
<p><figure id="attachment_2977" style="width: 618px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/FirstStepBruteforcestringmatching.png"><img src="/wp-content/uploads/2012/03/FirstStepBruteforcestringmatching.png" alt="First step of brute force string matching" title="First step of brute force string matching" width="618" height="236" class="size-full wp-image-2977" srcset="/wp-content/uploads/2012/03/FirstStepBruteforcestringmatching.png 618w, /wp-content/uploads/2012/03/FirstStepBruteforcestringmatching-300x114.png 300w" sizes="(max-width: 618px) 100vw, 618px" /></a><figcaption class="wp-caption-text">We start by comparing the first characters of the text and the pattern!</figcaption></figure> <span id="more-2966"></span><br />
If they don’t match we move forward the second character of the text. Now we compare the first character of the pattern with the second character of the text. If they don’t match again we move forward until we get a match or until we reach the end of the text. </p>
<figure id="attachment_2982" style="width: 612px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/SecondStepBruteforcestringmatching.png"><img src="/wp-content/uploads/2012/03/SecondStepBruteforcestringmatching.png" alt="Second step of brute force string matching" title="Second step of brute force string matching" width="612" height="241" class="size-full wp-image-2982" srcset="/wp-content/uploads/2012/03/SecondStepBruteforcestringmatching.png 612w, /wp-content/uploads/2012/03/SecondStepBruteforcestringmatching-300x118.png 300w" sizes="(max-width: 612px) 100vw, 612px" /></a><figcaption class="wp-caption-text">Because the first character of the text and the pattern don&#039;t match, we move forward the second character of the text. Now we compare the second character of the text with the first character of the pattern!</figcaption></figure>
<p>In case they match we move forward the second character of the pattern comparing it with the “next” character of the text, as on the picture bellow.</p>
<figure id="attachment_2981" style="width: 617px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/ThirdStepBruteforcestringmatching.png"><img src="/wp-content/uploads/2012/03/ThirdStepBruteforcestringmatching.png" alt="Third step of  brute force string matching" title="Third step of  brute force string matching" width="617" height="235" class="size-full wp-image-2981" srcset="/wp-content/uploads/2012/03/ThirdStepBruteforcestringmatching.png 617w, /wp-content/uploads/2012/03/ThirdStepBruteforcestringmatching-300x114.png 300w" sizes="(max-width: 617px) 100vw, 617px" /></a><figcaption class="wp-caption-text">If case a character from the text match against the first character of the pattern we move forward to the second character of the pattern and the next character of the text!</figcaption></figure>
<p>Just because we have found a match between the first character from the pattern with some character of the text, doesn’t mean that the pattern appears in the text. We must move forward to see whether the full pattern is contained into the text. </p>
<figure id="attachment_2980" style="width: 619px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/MatchBruteforcestringmatching.png"><img src="/wp-content/uploads/2012/03/MatchBruteforcestringmatching.png" alt="Match in brute force string matching" title="Match in brute force string matching" width="619" height="229" class="size-full wp-image-2980" srcset="/wp-content/uploads/2012/03/MatchBruteforcestringmatching.png 619w, /wp-content/uploads/2012/03/MatchBruteforcestringmatching-300x110.png 300w" sizes="(max-width: 619px) 100vw, 619px" /></a><figcaption class="wp-caption-text">The pattern is matched!</figcaption></figure>
<h2>Implementation</h2>
<p>Implementation of brute force string matching is easy and here we can see a short PHP example. The bad news is that naturally this algorithm is quite slow.</p>
<pre lang="PHP">
function sub_string($pattern, $subject) 
{
	$n = strlen($subject);
	$m = strlen($pattern);
	
	for ($i = 0; i < $n-$m; $i++) {
		$j = 0;
		while ($j < $m &#038;&#038; $subject[$i+$j] == $pattern[$j]) {
			$j++;
		}
		if ($j == $m) return $i;
	}
	return -1;
}

echo sub_string('o wo', 'hello world!');

</pre>
<h2>Complexity</h2>
<p>As I said this algorithm is slow. Actually every algorithm that contains “brute force” in its name is slow, but to show how slow is string matching I can say that its complexity is O(n.m). Here <strong>n</strong> is the length of the text, while <strong>m</strong> is the length of the pattern.</p>
<figure id="attachment_2978" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart1.png"><img src="/wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart1.png" alt="Brute force string matching complexity chart 1" title="Brute force string matching complexity chart 1" width="600" height="371" class="size-full wp-image-2978" srcset="/wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart1.png 600w, /wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart1-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text">For fixed pattern length of m = 5, we can see that even for relatively short text the time grows quickly!</figcaption></figure>
<p>In case we fix the length of the text and test against variable length of the pattern, again we get rapidly growing function.</p>
<figure id="attachment_2979" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart2.png"><img src="/wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart2.png" alt="Brute force string matching complexity chart 2" title="Brute force string matching complexity chart 2" width="600" height="371" class="size-full wp-image-2979" srcset="/wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart2.png 600w, /wp-content/uploads/2012/03/BruteForceStringMatchingComplexityChart2-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text"> </figcaption></figure>
<h2>Application</h2>
<p>Brute force string matching can be very ineffective, but it can also be very handy in some cases. Just like the <a href="/2011/11/24/computer-algorithms-sequential-search/" title="Computer Algorithms: Sequential Search">sequential search</a>.</p>
<h3>It can be very useful ...</h3>
<ol>
<li>Doesn't require pre-processing of the text - Indeed if we search the text only once we don't need to pre-process it. Most of the algorithms for string matching need to build an index of the text in order to search quickly. This is great when you've to search more than once into a text, but if you do only once, perhaps (for short texts) brute force matching is great!</li>
<li>Doesn't require additional space - Because brute force matching doesn't need pre-processing it also doesn't require more space, which is one cool feature of this algorithm</li>
<li>Can be quite effective for short texts and patterns</li>
</ol>
<h3>It can be ineffective ...</h3>
<ol>
<li>If we search more than once the text - As I said in the previous section if you perform the search more than once it's perhaps better to use another string matching algorithm that builds an index and it's faster.</li>
<li>It's slow - In general brute force algorithms are slow and brute force matching isn't an exception.</li>
</ol>
<h2>Final Words</h2>
<p>String matching is something very special in software development and it is used in various cases, so every developer must be familiar with this topic.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/04/02/computer-algorithms-rabin-karp-string-searching/" rel="bookmark" title="Computer Algorithms: Rabin-Karp String Searching">Computer Algorithms: Rabin-Karp String Searching </a></li>
<li><a href="/2012/04/09/computer-algorithms-morris-pratt-string-searching/" rel="bookmark" title="Computer Algorithms: Morris-Pratt String Searching">Computer Algorithms: Morris-Pratt String Searching </a></li>
<li><a href="/2012/04/17/computer-algorithms-boyer-moore-string-search-and-matching/" rel="bookmark" title="Computer Algorithms: Boyer-Moore String Searching">Computer Algorithms: Boyer-Moore String Searching </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/03/27/computer-algorithms-brute-force-string-matching/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
