<?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; string</title>
	<atom:link href="http://talkbinary.com/tag/string/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>
		<item>
		<title>Fundamental Data Type String</title>
		<link>http://talkbinary.com/programming/c/fundamental-data-type-string/</link>
		<comments>http://talkbinary.com/programming/c/fundamental-data-type-string/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 05:14:59 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=133</guid>
		<description><![CDATA[In this tutorial you shall learn how to use 1. String Data Type You should immediately notice that the declaration and assignment of a string is similar to int and doubles which you learned previously. Declaring and Initializing the String First make sure you have the appropriate headers in the file where you plan to<a class="moretag" href="http://talkbinary.com/programming/c/fundamental-data-type-string/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>In this tutorial you shall learn how to use<br />
<br/>1. String Data Type<br />
<br/>You should immediately notice that the declaration and assignment of a string is similar to int and doubles which you learned previously.<br />
<br/></p>
<h3 id="section-1">Declaring and Initializing the String</h3>
<p>First make sure you have the appropriate headers in the file where you plan to use string objects.</p>
<pre lang="c++">#include <string>
using namespace std; </pre>
<p><br/><br />
How to declare and initialize strings:</p>
<pre lang="c++">
string student = "Diego";    // Declare and initialize to Diego
student = "Talk Binary"; //Assign student to Talk Binary
</pre>
<p>Notice how strings are encapsulated within &#8220;&#8221;&#8221;s. If used without them, it would mean you would want to assign the value of that variable of that name to your string variable.<br />
<br/>From the terminal&#8230;<span id="more-133"></span></p>
<pre lang="c++">
cin >> student; //Reads up to the space
getline(cin, student); //Reads up to pressed Enter
</pre>
<p><br/>You may also concatenate strings</p>
<pre lang="c++">
string first = "Talk";
string last = "Binary";
string name = first + " " + last; //What happens if we exclude the " " ?
cout << name; //Outputs Talk Binary.
</pre>
<p><br/></p>
<h3 id="section-2">Substrings</h3>
<p><br/>We can find the length of a string using the length() function and also print out a substring from a string.</p>
<pre lang="c++">
string name = "Talk Binary";
cout << name.length() << endl; //Outputs 11 (Counts the space as a character).
cout << name.substr(3,5); //Starts at position 3, and prints 5 characters "k Bin"
</pre>
<p><br/>The position is determined as follows.<br />
<br/><center><img src="http://localhost/wordpress/wp-content/uploads/2008/06/1-string.jpg" alt="" title="1-string" width="274" height="58" class="alignnone size-full wp-image-7" /></center><br />
<br/>You may also use the following...</p>
<pre lang="c++">
cout << name[3]; //Will output the character in position 3.
</pre>
<p><br/>Remember, the best way to learn something, is to practice! Therefore, start practicing!<br />
<br/></p>
<h3 id="section-3">Programming Challenges</h3>
<p>Using what we know, we'll make a small program.<br />
<br/>1. Read in a first name, and last name, and assign them to a first and last string variable respectively. </p>
<ul>
<li>a. Find the length of both and assign their lengths to two different int variables respectively.</li>
<li>b. Concatenate the first and last name into one string variable.</li>
<li>c. Using the substr function, and your two int variables holding length, extract the first and last name from your last string variable that has the first and last name concatenated.</li>
</ul>
<p><br/><br />
<br/>If you have any questions, comments, or concerns please comment below!</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/fundamental-data-type-string/feed/</wfw:commentRss>
		<slash:comments>9</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:32:01 -->
