<?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>Talk Binary &#187; substr</title>
	<atom:link href="http://talkbinary.com/tag/substr/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C++ Recursion &#8211; Palindrome</title>
		<link>http://talkbinary.com/programming/c/c-recursion-palindrome/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion-palindrome/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 06:30:38 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ function]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[palindrome]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive function]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[substr]]></category>
		<category><![CDATA[substring]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2376</guid>
		<description><![CDATA[Determining if a string is a Palindrome using recursion Below is an example of how to determine if a word is a palindrome using recursion. A Palindrome is a string that could be read the same way in either direction such as racecar, mom, and even a. This tutorial expects you to understands the following.<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion-palindrome/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h2 id="section-1">Determining if a string is a Palindrome using recursion</h2>
<p>Below is an example of how to determine if a word is a palindrome using recursion. A <strong>Palindrome</strong> is a string that could be read the same way in either direction such as<em> racecar</em>,<em> mom</em>, and even <em>a</em>.  This tutorial expects you to understands the following.</p>
<p><a href="http://talkbinary.com/programming/c/introduction-to-recursion-in-c/">C++ Recursion</a></p>
<blockquote><h3 id="section-2">Example of finding if a word is a palindrome</h3>
<p>To determine if a word is a palindrome do the following.</p>
<ol>
<li>Compare the first and last character</li>
<li>If these characters are the same, disregard these characters and repeat until one one character or no characters remain</li>
</ol>
<p>Example:</p>
<pre lang="c++">

// Check first and last character
[ r ] [ a ] [ c ] [ e ] [ c ] [ a ] [ r ] 

// Second iteration
[ a ] [ c ] [ e ] [ c ] [ a ] 

// Third iteration
[ c ] [ e ] [ c ]  

// Fourth iteration
[ e ]

// The word is a palindrome.
</pre>
<p><br/><br />
Now let&#8217;s try one that isn&#8217;t a palindrome.</p>
<pre lang="c++">
// First iteration
[ t ] [ a ] [ b ] [ t ]

// In our second iteration, a and b don't match
[ a ] [ b ]</pre>
</blockquote>
<p><span id="more-2376"></span></p>
<h2 id="section-3"> Creating an is Palindrome function using recursion</h2>
<p>There are multiple ways to create a recursive function. Try coming up with your own before seeing the one below. As always, we&#8217;ll start with the function header.</p>
<blockquote><h3 id="section-4">Function header for is Palindrome</h3>
<p>We&#8217;ll be using the following function header. </p>
<pre lang="c++">bool isPalindrome(string word); </pre>
<p><br/>You may ask, do we need more parameters? Not necessarily. Think <strong>substr</strong>. </p></blockquote>
<blockquote><h3 id="section-5">Base Cases for is Palindrome</h3>
<p>If you recall we had three base cases. </p>
<ol>
<li>If the first and last character don&#8217;t match, the word is not a palindrome</li>
<li>If the word contains one or no characters, the word is a palindrome</li>
</ol>
<pre lang="c++">bool isPalindrome(string word) { 

      int strlen = word.size();

     // Base Case #1
     if ( word[0] != word[strlen - 1] )
         return false;

     // Base Cases #2, #3
     else if ( strlen <= 1 )
          return true;
</pre>
<p><br/>We store the length of the word so we don't have to be recomputing it more than once. You'll see why later.
</p></blockquote>
<blockquote><h3 id="section-6">Recursive call for is Palindrome</h3>
<p>If you also recall from our initial example, if the first and last character match, we disregard them in our next comparison. How do we call <strong>isPalindrome</strong> with this new word? Think <strong>substr</strong>.</p>
<h4 id="section-7">Substr returns a substring of a string</h4>
<p><strong>substr</strong> returns a substring of a string. </p>
<pre lang="c++">bool isPalindrome(string word) { 

      int strlen = word.size();

     // Base Case #1
     if ( word[0] != word[strlen - 1] )
         return false;

     // Base Cases #2, #3
     else if ( strlen <= 1 )
          return true;

     // At this point we know the characters matched
     else
          return isPalindrome(word.substr(1, strlen - 2));

}
</pre>
<p><br/> Why do we return a substring with the following parameters? If you don't recall how substr works, simply follow the next explanation. In essence, at each recursive call we are reducing the strings in the same way shown in the explanation.
</p></blockquote>
<h2 id="section-8">string::subtr</h2>
<p>The <strong>substr</strong> functions returns a substring with the following parameters of the current object. </p>
<blockquote><h3 id="section-9">Paramters of substr</h3>
<ul>
<li>pos - Start position of the character of the <em>current</em> word</li>
<li>n - Length of the substring starting from the start position <em>pos</em>.</li>
</ul>
<h4 id="section-10">Return value of substr</h4>
<p>A <strong>string</strong> containing the substring of the object.</p>
<h4 id="section-11">Example usage of substr</h4>
<pre lang="c++">string msg = "racecar";

cout << msg << endl;

// Our new substr should start at position 1 and be of size 5
// which is shown below.
//
// [ r ] [ a ] [ c ] [ e ] [ c ] [ a ] [ r ]
//        | S                              |
string new msg = msg.substr(1, msg.size()-2);

cout << newmsg << endl;</pre>
<p><br/>Will result in the following.</p>
<pre>racecar

aceca</pre>
<p><br/>This should make you understand how the recursive calls are made to determine if the word is a palindrome.
</p></blockquote>
<h3 id="section-12">Going further</h3>
<ol>
<li>Can you apply this using a vector?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion-palindrome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-02-11 13:21:29 -->
