<?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>C ++ &#187; String</title>
	<atom:link href="http://www.bizzymicbizness.com/cplusplus/tag/string/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bizzymicbizness.com/cplusplus</link>
	<description>All about C++</description>
	<lastBuildDate>Mon, 19 Oct 2009 12:38:17 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adventures in String Input</title>
		<link>http://www.bizzymicbizness.com/cplusplus/adventures-in-string-input</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/adventures-in-string-input#comments</comments>
		<pubDate>Sat, 19 Sep 2009 02:23:15 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[String Input]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=425</guid>
		<description><![CDATA[(A-1) shows that string input

Can be tricky


Example of (A-1)

// instr1.cpp -- reading more than one string
#include &#60;iostream&#62;
int main&#40;&#41;
&#123;
	using namespace std;
	const int ArSize = 20;
	char name&#91;ArSize&#93;;
	char dessert&#91;ArSize&#93;;
&#160;
	cout &#60;&#60; &#34;Enter your name:\n&#34;;
	cin &#62;&#62; name;
	cout &#60;&#60; &#34;Enter your favorite dessert:\n&#34;;
	cin &#62;&#62; dessert;
	cout &#60;&#60; &#34;I have some delicious &#34; &#60;&#60; dessert;
	cout &#60;&#60; &#34; for you, &#34; &#60;&#60; name &#60;&#60; [...]]]></description>
			<content:encoded><![CDATA[<p>(A-1) shows that string input</p>
<ul>
<li>Can be tricky</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// instr1.cpp -- reading more than one string</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> ArSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">20</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> name<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> dessert<span style="color: #008000;">&#91;</span>ArSize<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter your name:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> name<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter your favorite dessert:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> dessert<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;I have some delicious &quot;</span> <span style="color: #000080;">&lt;&lt;</span> dessert<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; for you, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
The intent of the program in (A-1) is simple:</p>
<ul>
<li>Read a user’s name and favorite dessert from the keyboard and then display the information (B-1)</li>
</ul>
<p><br class="blank" /><br />
Here is a sample run (A-1):</p>
<p> <img class="aligncenter size-full wp-image-431" title="Sample run" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/09/Sample-run.jpg" alt="Sample run" width="529" height="243" /><br />
<br class="blank" /><br />
Program Notes (A-1)</p>
<ul>
<li>(B-1) We didn’t even get a chance to respond to the dessert prompt (B-2)
<ul>
<li>(B-2) The program showed it and then immediately moved on to display the final line (B-3)</li>
</ul>
</li>
</ul>
<p><br class="blank" /> </p>
<ul>
<li>(B-3) The problem lies with how <strong>cin</strong> determines when you’ve finished entering a string (B-4)</li>
<ul>
<li>(B-4) You can’t enter the null character from the keyboard (B-5)</li>
<ul>
<li>(B-5)<strong> cin</strong> needs some other means for locating the end of a string (B-6)</li>
</ul>
<li>(B-6) The <strong>cin</strong> technique (B-7)</li>
<ul>
<li>(B-7) Is to use whitespace—spaces, tabs, and newlines—to delineate a string (B-8)</li>
<li>(B-8) This means <strong>cin</strong> reads just one word when it gets input for a character array (B-9)</li>
<li>(B-9) After it reads this word, <strong>cin</strong> automatically adds the terminating null character when it places the string into the array (B-10)</li>
</ul>
</ul>
</ul>
<p><br class="blank" /> </p>
<ul>
<li>(B-10) The practical result in this example (B-11)
<ul>
<li>(B-11) Is that <strong>cin</strong> reads <strong>Bjarne</strong> as the entire first string and puts it into the name array (B-12)
<ul>
<li>(B-12) This leaves poor <strong>Stroustrup</strong> still sitting in the input queue (B-13)</li>
<li>(B-13) When <strong>cin</strong> searches the input queue for the response to the favorite dessert question, it finds <strong>Stroustrup</strong> still there (B-14)</li>
<li>(B-14) Then <strong>cin</strong> gobbles up <strong>Stroustrup</strong> and puts it into the dessert array (Image-1)</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
(Image-1) The <strong>cin</strong> view of string input</p>
<p> <img class="aligncenter size-full wp-image-429" title="The cin view of string" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/09/The-cin-view-of-string.jpg" alt="The cin view of string" width="526" height="307" /></p>
<p><br class="blank" /><br />
Another problem, which didn’t surface in the sample run</p>
<ul>
<li>Is that the input string might turn out to be longer than the destination array</li>
<li>Using <strong>cin</strong> as this example did offer no protection against placing a 30-character string in a 20-character array</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/adventures-in-string-input/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Strings in an Array</title>
		<link>http://www.bizzymicbizness.com/cplusplus/using-strings-in-an-array</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/using-strings-in-an-array#comments</comments>
		<pubDate>Thu, 17 Sep 2009 14:06:24 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[strlen()]]></category>
		<category><![CDATA[Symbolic Constants]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=400</guid>
		<description><![CDATA[The two most common ways of getting a string into an array

Are to initialize an array to a string constant and to read keyboard or file input into an array
(A-1) demonstrates these approaches by initializing one array to a quoted string and using cin to place an input string in a second array

The program (A-1) [...]]]></description>
			<content:encoded><![CDATA[<p>The two most common ways of getting a string into an array</p>
<ul>
<li>Are to initialize an array to a string constant and to read keyboard or file input into an array</li>
<li>(A-1) demonstrates these approaches by initializing one array to a quoted string and using <strong>cin</strong> to place an input string in a second array
<ul>
<li>The program (A-1) also uses the standard library function <strong>strlen()</strong> to get the length of a string</li>
<li>The standard <strong>cstring</strong> header file (or <strong>string.h</strong> for older implementations) provides declarations for this and many other string-related functions (A-1)</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// strings.cpp -- storing strings in an array</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;cstring&gt; // for the strlen() function</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> kSize <span style="color: #000080;">=</span> <span style="color: #0000dd;">15</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">char</span> name1<span style="color: #008000;">&#91;</span>kSize<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// empty array</span>
	<span style="color: #0000ff;">char</span> name2<span style="color: #008000;">&#91;</span>kSize<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;C++omputer&quot;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// initialized array</span>
	<span style="color: #666666;">// NOTE: some implementations may require the static keyword</span>
	<span style="color: #666666;">// to initialize the array name2</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Howdy! I'm &quot;</span> <span style="color: #000080;">&lt;&lt;</span> name2<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;! What's your name?<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> name1<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Well, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> name1 <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, your name has &quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>name1<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; letters and is stored<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;in an array of &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>name1<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; bytes.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Your initial is &quot;</span> <span style="color: #000080;">&lt;&lt;</span> name1<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	name2<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #008080;">;</span> <span style="color: #666666;">// null character</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Here are the first 3 characters of my name: &quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> name2 <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Compatibility Note (A-1)</p>
<ul>
<li>If your system doesn’t provide the <strong>cstring</strong> header file, try the older <strong>string.h</strong> version</li>
</ul>
<p><br class="blank" /><br />
Here is a sample run of the program in (A-1):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-401" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/09/Output1.jpg" alt="Output" width="593" height="271" /></p>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
First, note that the <strong>sizeof</strong> operator gives the size of the entire array, 15 bytes, but the <strong>strlen()</strong> function returns the size of the string stored in the array and not the size of the array itself (B-1)</p>
<ul>
<li>(B-1) Also, <strong>strlen()</strong> counts just the visible characters and not the null character. Thus, it returns a value of <strong>6</strong>, not <strong>7</strong>, for the length of <strong>Bjarne</strong> (B-2)</li>
<li>(B-2) If <strong>Stroustrup</strong> is a string, the minimum array size for holding that string is <strong>strlen(Stroustrup)</strong> <strong>+ 1</strong></li>
</ul>
<p><br class="blank" /> <br />
(B-3) Because <strong>name1</strong> and <strong>name2</strong> are arrays, you can use an index to access individual characters in the array (B-4)</p>
<ul>
<li>(B-4) For example, the program uses <strong>name1[0]</strong> to find the first character in that array (B-5)</li>
<li>(B-5) Also, the program sets <strong>name2[3]</strong> to the null character. That makes the string end after three characters, even though more characters remain in the array (Image-1)</li>
</ul>
<p><br class="blank" /> <br />
(B-6) Note that the program in (A-1) uses a symbolic constant for the array size (B-7)</p>
<ul>
<li>(B-7) Often, the size of an array appears in several statements in a program (B-8)</li>
<li>(B-8) Using a symbolic constant to represent the size of an array simplifies revising the program to use a different array size; you just have to change the value once, where the symbolic constant is defined</li>
</ul>
<p><br class="blank" /><br />
(Image-1) Shortening a string with <strong>\0</strong></p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-402" title="Shortening a string" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/09/Shortening-a-string.jpg" alt="Shortening a string" width="554" height="414" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/using-strings-in-an-array/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

