<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[C++ Primer Aufgabe]]></title><description><![CDATA[<p>Wieder eine Aufgabe aus dem C++ Primer:<br />
Die Aufgabenstellung war, man soll 2 C-Strings miteinander vergleichen. (ich wollte dass ohne strcmp machen, viele werden nun wahrscheinlich von Zeitverschwnedung reden, aber es geht mir nur um die Übung, und ja ich weiß dass strcmp sicher besser und schneller ist, und ja in einem richtigen Programm verwende ich sowieso Strings <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> )</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    enum SIZE{nothing, s1_smaller, s2_smaller};

    //Zu vergleichende C-Strings
    char s1[] = &quot;Ab&quot;;
    char s2[] = &quot;Aaaa&quot;;
    SIZE type = nothing;

    size_t temp = 0;
    if(strlen(s1) &lt; strlen(s2)) temp = strlen(s1);
    else temp = strlen(s2);

    const size_t max_count = temp;

    for(size_t i=0; i&lt;max_count; ++i)
    {
        if(s1[i] &lt; s2[i])
        {
            type = s1_smaller;
            break;
        }
        else if(s2[i] &lt; s1[i])
        {
            type = s2_smaller;
            break;
        }
    }

    if(type == nothing)
    {
        if(strlen(s1) &lt; strlen(s2)) cout &lt;&lt; &quot;s1 KLEINER&quot; &lt;&lt; endl;
        else if(strlen(s2) &lt; strlen(s1)) cout &lt;&lt; &quot;s2 KLEINER&quot; &lt;&lt; endl;
        else cout &lt;&lt; &quot;s1 GLEICH s2&quot; &lt;&lt; endl;
    }
    else
    {
        if(type == s1_smaller) cout &lt;&lt; &quot;s1 KLEINER&quot; &lt;&lt; endl;
        if(type == s2_smaller) cout &lt;&lt; &quot;s2 KLEINER&quot; &lt;&lt; endl;
    }

    return 0;
}
</code></pre>
<p>Eure Meinung? Wie ist dass besser umsetzbar? Bekanntlich führen ja mehrere Wege zum Ziel.</p>
<p>MfG<br />
Stromberg</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/231088/c-primer-aufgabe</link><generator>RSS for Node</generator><lastBuildDate>Sat, 26 Sep 2026 00:31:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/231088.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 06 Jan 2009 15:20:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Primer Aufgabe on Tue, 06 Jan 2009 15:20:40 GMT]]></title><description><![CDATA[<p>Wieder eine Aufgabe aus dem C++ Primer:<br />
Die Aufgabenstellung war, man soll 2 C-Strings miteinander vergleichen. (ich wollte dass ohne strcmp machen, viele werden nun wahrscheinlich von Zeitverschwnedung reden, aber es geht mir nur um die Übung, und ja ich weiß dass strcmp sicher besser und schneller ist, und ja in einem richtigen Programm verwende ich sowieso Strings <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> )</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    enum SIZE{nothing, s1_smaller, s2_smaller};

    //Zu vergleichende C-Strings
    char s1[] = &quot;Ab&quot;;
    char s2[] = &quot;Aaaa&quot;;
    SIZE type = nothing;

    size_t temp = 0;
    if(strlen(s1) &lt; strlen(s2)) temp = strlen(s1);
    else temp = strlen(s2);

    const size_t max_count = temp;

    for(size_t i=0; i&lt;max_count; ++i)
    {
        if(s1[i] &lt; s2[i])
        {
            type = s1_smaller;
            break;
        }
        else if(s2[i] &lt; s1[i])
        {
            type = s2_smaller;
            break;
        }
    }

    if(type == nothing)
    {
        if(strlen(s1) &lt; strlen(s2)) cout &lt;&lt; &quot;s1 KLEINER&quot; &lt;&lt; endl;
        else if(strlen(s2) &lt; strlen(s1)) cout &lt;&lt; &quot;s2 KLEINER&quot; &lt;&lt; endl;
        else cout &lt;&lt; &quot;s1 GLEICH s2&quot; &lt;&lt; endl;
    }
    else
    {
        if(type == s1_smaller) cout &lt;&lt; &quot;s1 KLEINER&quot; &lt;&lt; endl;
        if(type == s2_smaller) cout &lt;&lt; &quot;s2 KLEINER&quot; &lt;&lt; endl;
    }

    return 0;
}
</code></pre>
<p>Eure Meinung? Wie ist dass besser umsetzbar? Bekanntlich führen ja mehrere Wege zum Ziel.</p>
<p>MfG<br />
Stromberg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1640452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1640452</guid><dc:creator><![CDATA[Stromberg]]></dc:creator><pubDate>Tue, 06 Jan 2009 15:20:40 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Primer Aufgabe on Tue, 06 Jan 2009 15:32:55 GMT]]></title><description><![CDATA[<p>Stromberg schrieb:</p>
<blockquote>
<p>Wieder eine Aufgabe aus dem C++ Primer:<br />
Die Aufgabenstellung war, man soll 2 C-Strings miteinander vergleichen. (ich wollte dass ohne strcmp machen</p>
</blockquote>
<p>sachon klar. wirst gleich hören, daß du unbedingt str<strong>n</strong>cmp nehmen sollst. weil das sicherer ist. dafür wird man mir sagen, daß ich verfrüht optimiere.</p>
<p>ich zeig dir mal die übliche implementierung (evtl ein fehler drin):</p>
<pre><code class="language-cpp">//erstmal laufen, bis einer aufhört oder beide ungleich
while(s1[i]!='\0' and s1[i]==s2[i])
   i=i+1;

//und jetzt ein abschlußtrick
int ergebnis=s2[i]-s1[i];

if(ergebnis&gt;0)
  cout&lt;&lt;&quot;s2 war größer&quot;;
else if(ergebnis&lt;0)
  cout&lt;&lt;&quot;s1 war größer&quot;;
else//ergebnis==0
  cout&lt;&lt;&quot;beide gleich&quot;;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1640462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1640462</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 06 Jan 2009 15:32:55 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Primer Aufgabe on Tue, 06 Jan 2009 15:55:32 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>dafür wird man mir sagen, daß ich verfrüht optimiere.</p>
<p>ich zeig dir mal die übliche implementierung (evtl ein fehler drin):</p>
<pre><code class="language-cpp">//erstmal laufen, bis einer aufhört oder beide ungleich
while(s1[i]!='\0' and s1[i]==s2[i])
   i=i+1;
</code></pre>
</blockquote>
<p>also wirklich i=i+1 <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /> , da musst du schon zeiegn, dass du es mindestens so gut wie der compiler kannst, also i++ und falls einer size_t überladen hat, gleich noch ++i. :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1640477</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1640477</guid><dc:creator><![CDATA[gibts ja nicht]]></dc:creator><pubDate>Tue, 06 Jan 2009 15:55:32 GMT</pubDate></item></channel></rss>