<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://www.linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Thu May 23, 2013 10:34 pm by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Thu May 23, 2013 10:34 pm</pubDate>
  <lastBuildDate>Thu May 23, 2013 10:34 pm</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://www.linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: Trying to write a simple string reversal function in C++</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=101569#101569</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=67058'&gt;larcky&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Sun Sep 11, 2011 9:33 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi&lt;br /&gt;
It's a shame you're not using real C++ strings (as you say you want to learn it the hard way) because then it's easy:&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main&amp;#40;&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; std&amp;#58;&amp;#58;string s1&amp;#40; &amp;quot;Here is a string...&amp;quot; &amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; std&amp;#58;&amp;#58;string s2&amp;#40; s1 &amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; std&amp;#58;&amp;#58;copy&amp;#40; s1.rbegin&amp;#40;&amp;#41;, s1.rend&amp;#40;&amp;#41;, s2.begin&amp;#40;&amp;#41; &amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; std&amp;#58;&amp;#58;cout &amp;lt;&amp;lt; &amp;quot;s2=&amp;quot; &amp;lt;&amp;lt; s2 &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; return 0;&lt;br /&gt;
&lt;br /&gt;
&amp;#125; // main&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
For C-style strings I think I'd rather use 'do...while' than a 'for' loop.  Just seems a bit easier to read when you've got two indices flying about&amp;#058;&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main&amp;#40;&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; const char* s1 = &amp;quot;Here is a string...&amp;quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; char s2&amp;#91; strlen&amp;#40;s1&amp;#41; + 1 &amp;#93;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; // Be sure to start at final '.' rather than null.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; int from = strlen&amp;#40;s1&amp;#41; - 1;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; int to&amp;nbsp; &amp;nbsp;= 0;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; do&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; s2&amp;#91;to&amp;#93; = s1&amp;#91;from&amp;#93;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ++to;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; --from;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;#125; while &amp;#40; from &amp;gt;= 0 &amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; s2&amp;#91;to&amp;#93; = '\0';&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; std&amp;#58;&amp;#58;cout &amp;lt;&amp;lt; &amp;quot;s2=&amp;quot; &amp;lt;&amp;lt; s2 &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; return 0;&lt;br /&gt;
&lt;br /&gt;
&amp;#125; // main&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
Regards, larcky</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=101569#101569</comments>
                                        <author>larcky</author>
                                        <pubDate>Sun Sep 11, 2011 9:33 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=101569#101569</guid>
                                      </item>
                                      <item>
                                        <title>Re: Trying to write a simple string reversal function in C++</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=101272#101272</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=67663'&gt;pajo_ojap&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Aug 22, 2011 3:44 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      A thought popped into my head and I realized there was a different approach with using the null terminator.  Since the string that its going into is already null terminated and the second loop structure  &lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;j = len;&lt;br /&gt;
for &amp;#40;int i = 0; i &amp;lt;= j; i++&amp;#41;&lt;br /&gt;
s&amp;#91;i&amp;#93; = temp&amp;#91;i&amp;#93;; &lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt; is set up to run from 0 to string length, you will need the line &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;temp&amp;#91;i&amp;#93; = '\0';&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt; as stated originally.  but if you modify the second loop to only run from 0 to strength length - 1 you wouldn't need to add the null termination because it already exists in variable memeber 's' at the correct spot.&lt;br /&gt;
&lt;br /&gt;
Hoping this is helpful</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=101272#101272</comments>
                                        <author>pajo_ojap</author>
                                        <pubDate>Mon Aug 22, 2011 3:44 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=101272#101272</guid>
                                      </item>
                                      <item>
                                        <title>Re: Trying to write a simple string reversal function in C++</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=101246#101246</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=67663'&gt;pajo_ojap&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Mon Aug 22, 2011 1:02 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      I'm kinda surprised no one has responded to this yet.  But here goes.  The main issue is the dreaded array index vs array size.  In member reverse you set local variable 'j' to 'len' which is the length of the character array stored in your string object.  You then use 'j' as the index to take from the back end of the stored string and save it into the front end of the local 'temp'.  Array indexes start at 0, so for a string length of 16, you want your indexing to run from 0 to 15 ( or in this case from 15 to 0).  What you are doing is starting at the null terminator of the string and working to one less than the front of the string (from 16 to 1) so you would never capture the first character.  This is also why nothing was being printed as the first element of your reversed character array is the null terminator.  This then brings up the next issue you would run into where the character string would not be properly null terminated for printing purposes (unless you wrote your own print to use the objects 'len' member).&lt;br /&gt;
&lt;br /&gt;
So you need to set &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;int j = len - 1&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt; in member function reverse and then you have to set &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;temp&amp;#91;i&amp;#93; = '\0';&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt; after the for loop that reverses the array.&lt;br /&gt;
&lt;br /&gt;
I compiled the code (with changes made for the missing parts to get it to compile) and it worked fine for me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Good luck with it.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=101246#101246</comments>
                                        <author>pajo_ojap</author>
                                        <pubDate>Mon Aug 22, 2011 1:02 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=101246#101246</guid>
                                      </item>
                                      <item>
                                        <title>Trying to write a simple string reversal function in C++</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=101240#101240</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=66284'&gt;stuarte9&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Sat Aug 20, 2011 6:25 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi,&lt;br /&gt;
&lt;br /&gt;
As the header says, I'm trying to write a simple(!) string reversal function in C++.&lt;br /&gt;
&lt;br /&gt;
First , here is the class:-&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;class string&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;public&amp;#58;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; // universal access&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;void assign&amp;#40;const char *st&amp;#41; &amp;#123; strcpy&amp;#40;s, st&amp;#41;; len = strlen&amp;#40;st&amp;#41;; &amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;int&amp;nbsp; length&amp;#40;&amp;#41; &amp;#123; return &amp;#40;len&amp;#41;; &amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;void print&amp;#40;&amp;#41; &amp;#123; std&amp;#58;&amp;#58;cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; &amp;quot;\nLength&amp;#58; &amp;quot; &amp;lt;&amp;lt; len &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;void reverse&amp;#40;&amp;#41;;&amp;nbsp; &amp;nbsp; &amp;nbsp;// Reverses the contents of hidden data member s.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;friend string operator+&amp;#40;const string&amp;amp; a, const string&amp;amp; b&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;private&amp;#58;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; // restricted access to member functions&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;char s&amp;#91;max_len&amp;#93;;&amp;nbsp; &amp;nbsp; // implementation by char&amp;#40;acter&amp;#41; array&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;int&amp;nbsp; len;&lt;br /&gt;
&amp;#125;;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Second, here is the relevant part of main() ;-&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;// Next two lines for debug only.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;std&amp;#58;&amp;#58;cout &amp;lt;&amp;lt; &amp;quot;Checking the contents of string one.&amp;quot; &amp;lt;&amp;lt; std&amp;#58;&amp;#58;endl;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;one.print&amp;#40;&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;std&amp;#58;&amp;#58;cout &amp;lt;&amp;lt; std&amp;#58;&amp;#58;endl &amp;lt;&amp;lt; std&amp;#58;&amp;#58;endl;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;one.reverse&amp;#40;&amp;#41;;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Reverse contents of string&lt;br /&gt;
&amp;nbsp; &amp;nbsp;one.print&amp;#40;&amp;#41;;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
Finally, here is the definition for member function reverse():-&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;void string&amp;#58;&amp;#58;reverse&amp;#40;&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;char temp&amp;#91;len&amp;#93;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;int j = len;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Work forwards from beginning of temp.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Work backwards from end of s.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;for &amp;#40;int i = 0; i &amp;lt;= len; i++, j--&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;temp&amp;#91;i&amp;#93; = s&amp;#91;j&amp;#93;;&lt;br /&gt;
&lt;br /&gt;
// Next two lines for debug only.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;std&amp;#58;&amp;#58;cout &amp;lt;&amp;lt; &amp;quot;\nChecking the contents of array temp.\n&amp;quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;std&amp;#58;&amp;#58;cout &amp;lt;&amp;lt; temp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;j = len;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;for &amp;#40;int i = 0; i &amp;lt;= j; i++&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;s&amp;#91;i&amp;#93; = temp&amp;#91;i&amp;#93;;&lt;br /&gt;
&amp;#125;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
The code compiles without any errors or warnings. However, when I execute the program I get the following output (partial listing only, the rest of the program works just fine):-&lt;br /&gt;
&lt;br /&gt;
Checking the contents of string one.&lt;br /&gt;
My name is Alan Turing.&lt;br /&gt;
Length: 23&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Checking the contents of array temp.&lt;br /&gt;
&lt;br /&gt;
Length: 16&lt;br /&gt;
&lt;br /&gt;
As nothing is printed for array temp I'm assuming that it has not received anything from hidden data member s. (Both are array of char and should be assignment compatible.) Is my understanding correct ? As reverse() is a member function it should have access to private data member &amp;quot;s&amp;quot;. Am I overlooking something obvious here ?&lt;br /&gt;
&lt;br /&gt;
I'm wanting to do this in this manner rather than use a library function as I'm just getting started.&lt;br /&gt;
&lt;br /&gt;
Any help that can be given with this would be very much appreciated.&lt;br /&gt;
&lt;br /&gt;
Thanks in advance.&lt;br /&gt;
&lt;br /&gt;
Stuart</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=101240#101240</comments>
                                        <author>stuarte9</author>
                                        <pubDate>Sat Aug 20, 2011 6:25 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=101240#101240</guid>
                                      </item></channel></rss>