<?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[wordcount mit string]]></title><description><![CDATA[<p>Hallo alle zusammen !<br />
Hoffe hierbei kann mir jemand behilflich sein!<br />
Aufgabe:<br />
Der Funktion wordcount wird ein String als Argument u¨bergeben. Die Funktion liefert<br />
die Zahl der im String enthaltenen Wörter zurück (Wörter sind durch Leerzeichen getrennt).<br />
Erstellen Sie den Prototypen (Funktionskopf) und schreiben Sie die Funktion.<br />
Habe versucht diese Aufgabe zu lösen jedoch ohne Erfolge.<br />
So weit bin ich gekommen:</p>
<pre><code>#include&lt;iostream&gt;
#include&lt;cstdlib&gt;
#include&lt;string&gt;
using namespace std;
void wordcount(string satz);

void main()
{
	string satz;
	cout&lt;&lt;&quot;Geben Sie einen Satz ein: &quot;;
	cin&gt;&gt;satz;
	wordcount(satz);
	system(&quot;PAUSE&quot;);
}

void wordcount(string satz)
{
	int words=0;
	int L=satz.length();
	for(int i=0;i&lt;L;i++)
	{
		if(satz.at(i)==' ')
		{
			words=words+1;
		}
	}
	cout&lt;&lt;&quot;Sie haben &quot;&lt;&lt;words&lt;&lt;&quot; Woerter eingegeben&quot;&lt;&lt;endl;
}
</code></pre>
<p>Denke das einzigste Problem Hier sind die Schleifen der Funktion. Hoffe jemand von euch kann mir hierbei helfen.<br />
Danke schon einmal an Alle.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/316974/wordcount-mit-string</link><generator>RSS for Node</generator><lastBuildDate>Tue, 28 Jul 2026 18:48:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/316974.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 23 May 2013 10:22:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 10:22:05 GMT]]></title><description><![CDATA[<p>Hallo alle zusammen !<br />
Hoffe hierbei kann mir jemand behilflich sein!<br />
Aufgabe:<br />
Der Funktion wordcount wird ein String als Argument u¨bergeben. Die Funktion liefert<br />
die Zahl der im String enthaltenen Wörter zurück (Wörter sind durch Leerzeichen getrennt).<br />
Erstellen Sie den Prototypen (Funktionskopf) und schreiben Sie die Funktion.<br />
Habe versucht diese Aufgabe zu lösen jedoch ohne Erfolge.<br />
So weit bin ich gekommen:</p>
<pre><code>#include&lt;iostream&gt;
#include&lt;cstdlib&gt;
#include&lt;string&gt;
using namespace std;
void wordcount(string satz);

void main()
{
	string satz;
	cout&lt;&lt;&quot;Geben Sie einen Satz ein: &quot;;
	cin&gt;&gt;satz;
	wordcount(satz);
	system(&quot;PAUSE&quot;);
}

void wordcount(string satz)
{
	int words=0;
	int L=satz.length();
	for(int i=0;i&lt;L;i++)
	{
		if(satz.at(i)==' ')
		{
			words=words+1;
		}
	}
	cout&lt;&lt;&quot;Sie haben &quot;&lt;&lt;words&lt;&lt;&quot; Woerter eingegeben&quot;&lt;&lt;endl;
}
</code></pre>
<p>Denke das einzigste Problem Hier sind die Schleifen der Funktion. Hoffe jemand von euch kann mir hierbei helfen.<br />
Danke schon einmal an Alle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325448</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325448</guid><dc:creator><![CDATA[Phil123]]></dc:creator><pubDate>Thu, 23 May 2013 10:22:05 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 10:26:24 GMT]]></title><description><![CDATA[<p>Ah und es müsste</p>
<pre><code>int words=1;
</code></pre>
<p>heißen da es ja immer ein Leerzeichen weniger als Worte sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325450</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325450</guid><dc:creator><![CDATA[Phil123]]></dc:creator><pubDate>Thu, 23 May 2013 10:26:24 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 10:32:55 GMT]]></title><description><![CDATA[<p>Gegeben Dein Algorithmus ist richtig:<br />
Ein großes Problem ist, dass der <code>operator&gt;&gt;()</code> nur bis zum ersten Leerzeichen liest.</p>
<p>Du schreibst entweder einen string in den Source</p>
<pre><code>std::string satz = &quot;Furble Wurble war hier.&quot;;
</code></pre>
<p>Oder Du liest mit der Funktion <code>getline()</code> ein:</p>
<pre><code>std::string satz;
std::getline(std::cin, satz);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2325452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325452</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Thu, 23 May 2013 10:32:55 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 10:36:43 GMT]]></title><description><![CDATA[<p>Ich nehme mal an, &quot;ohne Erfolg&quot; bezieht sich darauf, dass du immer 1 erhältst, oder? Das liegt nicht an deiner Funktion (die zählt zwar falsch, aber sie zählt), sondern wie du die Eingabe durchführst. Lies mal aufmerksam die Referenz durch:<br />
<a href="http://www.cplusplus.com/reference/string/string/operator%3E%3E/" rel="nofollow">http://www.cplusplus.com/reference/string/string/operator&gt;&gt;/</a></p>
<p>Zu deiner Zählfunktion an sich: Teste sie auch in folgenden Fällen:<br />
-Keine Wörter<br />
-Genau ein Wort<br />
-Zwei oder mehr Leerzeichen zwischen Wörtern<br />
-Leerzeichen am Anfang und/oder am Ende</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325456</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325456</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 23 May 2013 10:36:43 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 10:36:40 GMT]]></title><description><![CDATA[<p>Vielen Dank für die schnelle Antwort <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2325457</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325457</guid><dc:creator><![CDATA[Phil123]]></dc:creator><pubDate>Thu, 23 May 2013 10:36:40 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 10:41:14 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a><br />
Wenn ich aber die funktion und alles so beibehalte und dafür den string gleich definiere. Bekomme ich die richtige Wortanzahl heraus.<br />
Das sagt mir doch dass ansonsten alles in ordnung ist oder ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325460</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325460</guid><dc:creator><![CDATA[Phil123]]></dc:creator><pubDate>Thu, 23 May 2013 10:41:14 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 10:50:37 GMT]]></title><description><![CDATA[<pre><code>void wordcount(string satz)
{
    int words=0;
    int L=satz.length();
    for(int i=0;i&lt;L;i++)
    {
        if(satz.at(i)==' ')
        {
            words=words+1;
        }
    }
    cout&lt;&lt;&quot;Sie haben &quot;&lt;&lt;words&lt;&lt;&quot; Woerter eingegeben&quot;&lt;&lt;endl;
}
</code></pre>
<p>---&gt;</p>
<pre><code>#include &lt;algorithm&gt;

void wordcount(string satz)
{
    int words = count(satz.begin(), satz.end(), ' ');
    cout&lt;&lt;&quot;Sie haben &quot;&lt;&lt;words&lt;&lt;&quot; Woerter eingegeben&quot;&lt;&lt;endl;
}
</code></pre>
<p><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>
]]></description><link>https://www.c-plusplus.net/forum/post/2325463</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325463</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Thu, 23 May 2013 10:50:37 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 11:13:20 GMT]]></title><description><![CDATA[<p>Phil123 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a><br />
Wenn ich aber die funktion und alles so beibehalte und dafür den string gleich definiere. Bekomme ich die richtige Wortanzahl heraus.<br />
Das sagt mir doch dass ansonsten alles in ordnung ist oder ?</p>
</blockquote>
<p>Wieso bekomme ich dann gänzlich falsche Ergebnisse?</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/26744">@Ethon</a>: Ähnlicher Fehler wie der TE</p>
<p>Siehe:<br />
<a href="https://ideone.com/XJk11I" rel="nofollow">https://ideone.com/XJk11I</a><br />
Viele von den Beispielen sind nicht einmal besonders gemein. Das heißt natürlich nicht, dass man die gemeinen Fälle nicht auch richtig behandeln sollte. Das Testen der Extremfälle ist in der Programmierung sehr oft eine gute Methode, um Fehler zu finden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325468</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325468</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 23 May 2013 11:13:20 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 11:52:47 GMT]]></title><description><![CDATA[<p>schöne Gegenüberstellung. Macht mal wieder deutlich, dass man sich vorher oft nicht ausreichend Gedanken über mögliche Probleme macht.</p>
<p>Also vorher den String trimmen wie z.B. hier<br />
<a href="http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring" rel="nofollow">http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring</a><br />
beschrieben?</p>
<p>hab nicht gesehen, dass es schon etwas adäquates in std:: gäbe. Eigentlich erstaunlich, gerade mit LTRIM<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>(</mo><mi>R</mi><mi>T</mi><mi>R</mi><mi>I</mi><mi>M</mi></mrow><annotation encoding="application/x-tex">(RTRIM</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mopen">(</span><span class="mord mathit" style="margin-right:0.00773em;">R</span><span class="mord mathit" style="margin-right:0.13889em;">T</span><span class="mord mathit" style="margin-right:0.00773em;">R</span><span class="mord mathit" style="margin-right:0.07847em;">I</span><span class="mord mathit" style="margin-right:0.10903em;">M</span></span></span></span>(string)) konnte man als BASIC'ler schon immer &quot;angeben&quot; <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2325484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325484</guid><dc:creator><![CDATA[Quisslich]]></dc:creator><pubDate>Thu, 23 May 2013 11:52:47 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 12:01:19 GMT]]></title><description><![CDATA[<p>Ich wollte ihm nur std::count zeigen und habe seinen Code darauf angepasst - nicht mehr. Optimal ist es natürlich Wortanfänge zu zählen, also:</p>
<p>Ist der Charakter alphabetisch?<br />
---&gt; Ja<br />
Ist das Zeichen davor ein Whitespace oder außerhalb des Input Strings?<br />
---&gt; Ja =&gt; Wortzähler erhöhen.<br />
---&gt; Nein =&gt; Nichts tun.<br />
---&gt; Nein =&gt; Nichts tun.</p>
<p>Was man natürlich noch verbessern könnte um sinnlose Tests zu vermeiden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325489</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325489</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Thu, 23 May 2013 12:01:19 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 12:05:16 GMT]]></title><description><![CDATA[<p>Quisslich schrieb:</p>
<blockquote>
<p>Also vorher den String trimmen wie z.B. hier<br />
<a href="http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring" rel="nofollow">http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring</a><br />
beschrieben?</p>
</blockquote>
<p>Nein, Trimmen geht am Problem vorbei. Das eigentliche Problem ist, dass aus der Definition des Wortes &quot;Wörter sind durch Leerzeichen getrennt&quot; die falschen Schlüsse gezogen wurden, wie man Wörter erkennt. Zählen der Leerzeichen ist der falsche Weg, denn die Anzahl der trennenden Zeichen hat nicht direkt damit zu tun, wie viele getrennte Wörter es gibt. Der Versuch nun den String zu trimmen und tausend Fallunterscheidungen einzubauen, behebt bloß die Symptome des Problems.</p>
<p>Der bessere Weg wäre, die Trennungen, nicht die Trennzeichen zu zählen. Damit haben wir ebenfalls einen sehr einfachen Algorithmus, der aber richtig zählt:<br />
<a href="https://ideone.com/eL8Mrb" rel="nofollow">https://ideone.com/eL8Mrb</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325492</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 23 May 2013 12:05:16 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 13:51:28 GMT]]></title><description><![CDATA[<p>Ein Vorschlag:</p>
<pre><code class="language-cpp">#include &lt;cctype&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;boost/range/adaptors.hpp&gt;
#include &lt;boost/range/algorithm.hpp&gt;

int word_count(std::string const&amp; s)
{
   namespace ba = boost::adaptors;
   return boost::count(
     s | ba::transformed(static_cast&lt;int(*)(int)&gt;(std::isspace))
       | ba::uniqued,
     0
   );
}

int main()
{
    std::string test = &quot;Hallo  Welt,\n&quot;
              &quot;\tdu     grausame     Welt,\n&quot;
              &quot;\thast Tabs und auch Zeilen\n&quot;
              &quot;\tsowie      Steuerzeichen.&quot;;
    std::cout &lt;&lt; word_count(test) &lt;&lt; '\n';
    return 0;
}
</code></pre>
<p>getestet:</p>
<pre><code>12
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2325500</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325500</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 23 May 2013 13:51:28 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 12:33:08 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Der bessere Weg wäre, die Trennungen, nicht die Trennzeichen zu zählen. Damit haben wir ebenfalls einen sehr einfachen Algorithmus, der aber richtig zählt:<br />
<a href="https://ideone.com/eL8Mrb" rel="nofollow">https://ideone.com/eL8Mrb</a></p>
</blockquote>
<p>Igitt, eine for-Schleife.</p>
<pre><code class="language-cpp">int wordcount_Unreg(string satz)
{
  struct info { int w; bool l; } init{0,true};
  return std::accumulate(satz.begin(), satz.end(), init,
      [](info i,char c){bool b=isspace(c); return info{i.w+(!b&amp;&amp;i.l),b};}).w;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2325506</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325506</guid><dc:creator><![CDATA[random_unreg]]></dc:creator><pubDate>Thu, 23 May 2013 12:33:08 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 13:32:02 GMT]]></title><description><![CDATA[<p>Solange nur Leerzeichen auftauchen, ist das schön und gut, aber...</p>
<pre><code class="language-cpp">#include&lt;algorithm&gt;
#include&lt;cstdlib&gt;
#include&lt;iostream&gt;
#include&lt;iterator&gt;
#include&lt;sstream&gt;
#include&lt;string&gt;

using namespace std;

int wordcount_TE(string satz)
{
    int words=1;
    int L=satz.length();
    for(int i=0;i&lt;L;i++)
    {
        if(satz.at(i)==' ')
        {
            words=words+1;
        }
    }
    return words;
}

int wordcount_Ethon(string satz)
{
   int words = count(satz.begin(), satz.end(), ' ');
   return words;
}

int wordcount_SeppJ(string satz)
{
    int words=0;
    bool last_was_space = true;
    for(char c : satz)
    {
        if(c != ' ')
        {
            if (last_was_space)
               words += 1;
             last_was_space = false;
        }
        else
          last_was_space = true;
    }
    return words;
}

int wordcount_seldon(string satz) {
  istringstream in(satz);
  return distance(istream_iterator&lt;string&gt;(in), istream_iterator&lt;string&gt;());
}

void vergleich(string satz)
{
    cout &lt;&lt; &quot;Satz ist: \&quot;&quot; &lt;&lt; satz &lt;&lt; &quot;\&quot;\n&quot;
         &lt;&lt; &quot; Phil123 zählt darin &quot;       &lt;&lt; wordcount_TE(satz)
         &lt;&lt; &quot; Wörter, Ethon zählt &quot;       &lt;&lt; wordcount_Ethon(satz)
         &lt;&lt; &quot; Wörter, SeppJ zählt &quot;       &lt;&lt; wordcount_SeppJ(satz)
         &lt;&lt; &quot; Wörter, seldon zählt &quot;      &lt;&lt; wordcount_seldon(satz)
         &lt;&lt; '\n';
}

int main()
{
    vergleich(&quot;Hallo Welt&quot;);
    vergleich(&quot;&quot;);
    vergleich(&quot; &quot;);
    vergleich(&quot;  &quot;);
    vergleich(&quot;Hallo  Welt&quot;);
    vergleich(&quot;Hallo&quot;);
    vergleich(&quot; Hallo&quot;);
    vergleich(&quot;Hallo &quot;);
    vergleich(&quot; Hallo &quot;);
    vergleich(&quot;Hallo  Welt,\n&quot;
              &quot;\tdu     grausame     Welt,\n&quot;
              &quot;\thast Tabs und auch Zeilen\n&quot;
              &quot;\tsowie      Steuerzeichen.&quot;);
}
</code></pre>
<pre><code>Satz ist: &quot;Hallo  Welt,
	du     grausame     Welt,
	hast Tabs und auch Zeilen
	sowie      Steuerzeichen.&quot;
 Phil123 zählt darin 23 Wörter, Ethon zählt 22 Wörter, SeppJ zählt 9 Wörter, seldon zählt 12
</code></pre>
<p>Richtig spannend wird das allerdings erst, wenn auch Satzzeichen, Bindestriche und derlei berücksichtigt werden sollen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325519</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325519</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Thu, 23 May 2013 13:32:02 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 13:45:24 GMT]]></title><description><![CDATA[<p>Die Aufgabenstellung war eindeutig nur mit Leerzeichen. Aber alle Lösungen, außer Ethons, sind flexibel was die Definition der Trennung angeht und können leicht auf ein isspace umgestellt werden.</p>
<p>Flexibilität ist übrigens auch bei dir eine Schwäche. Die Lösung des TE (nach Korrektur), meine, krümelkackers (nach Korrektur) und die des Unregs sind auch leicht anpassbar, Satzzeichen nicht als Wörter zu zählen. Bei dir müsste das auch gehen, aber dazu wären wohl tiefgehende Fummeleien in der Locale nötig, die hier im Forum vielleicht ein oder zwei Personen beherrschen. Umgekehrt kann deine Lösung sich gar nicht an die eigentliche Definition (nur Leerzeichen) halten, außer wieder durch fortgeschrittene Locale-Manipulation.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325522</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 23 May 2013 13:45:24 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 14:04:22 GMT]]></title><description><![CDATA[<p>Da ist was wahres dran. Gut, ich nehme alles zurück.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325527</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325527</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Thu, 23 May 2013 14:04:22 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 14:05:51 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>krümelkackers (nach Korrektur)</p>
</blockquote>
<p>Tja, <code>boost::adaptors::transformed</code> (Boost 1.53) mag doch tatsächlich keime Lambdas, da sie nicht sowas wie einen <code>result_type</code> -typedef haben. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325528</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325528</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 23 May 2013 14:05:51 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 14:09:54 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>krümelkackers (nach Korrektur)</p>
</blockquote>
<p>Tja, <code>boost::adaptors::transformed</code> (Boost 1.53) mag doch tatsächlich keime Lambdas, da sie nicht sowas wie einen <code>result_type</code> -typedef haben. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
</blockquote>
<p>Da ist übrigens noch ein anderer Fehler drin. Aber den editierst du einfach raus, dann erzähle ich auch niemandem, was dir da peinliches passiert ist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /> .</p>
<p>edit: Oh, ich sehe, das hattest du ohnehin schon geändert. Dann hat's sich erledigt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325530</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325530</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 23 May 2013 14:09:54 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 14:46:55 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Bei dir müsste das auch gehen, aber dazu wären wohl tiefgehende Fummeleien in der Locale nötig, die hier im Forum vielleicht ein oder zwei Personen beherrschen.</p>
</blockquote>
<p>Das geht zwar (copy/paste von <a href="http://en.cppreference.com/w/cpp/locale/ctype_char" rel="nofollow">http://en.cppreference.com/w/cpp/locale/ctype_char</a> beherrschen bis auf ein, zwei Personen alle hier im Forum), ist aber nicht mal nötig.</p>
<pre><code class="language-cpp">struct mystr {};
std::istream&amp; operator&gt;&gt;(std::istream&amp; in, mystr&amp;)
{ std::istreambuf_iterator&lt;char&gt; it(in), end;
  std::find_if(std::find_if(it,end,[](char c){return  isspace(c);}),
               end,                [](char c){return !isspace(c);});
  return in;
}
int wordcount_fummler(string satz) {
  istringstream in(satz);
  return distance(istream_iterator&lt;mystr&gt;(in), istream_iterator&lt;mystr&gt;());
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2325541</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325541</guid><dc:creator><![CDATA[fummler]]></dc:creator><pubDate>Thu, 23 May 2013 14:46:55 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 15:05:27 GMT]]></title><description><![CDATA[<p>Edit: Yikes! Da hat schon jemand auf eine praktisch gleiche Lösung verlinkt!<br />
:duck und weg:</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325544</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325544</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 23 May 2013 15:05:27 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 20:46:24 GMT]]></title><description><![CDATA[<p>Uihh! - ein kleiner Programmier-Contest; da darf mein Beitrag nicht fehlen. <code>std::inner_product</code> hat noch keiner:</p>
<pre><code>#include &lt;functional&gt; // std::plus&lt;&gt;
#include &lt;iostream&gt;
#include &lt;numeric&gt; // std::inner_product
#include &lt;string&gt;
#include &lt;cctype&gt; // std::isalnum, etc.

namespace werner
{
    int wordcount( const std::string&amp; satz )
    {
        return std::inner_product( begin(satz), end(satz)-1, begin(satz)+1, !satz.empty() &amp;&amp; std::isalnum( satz.front() )? 1: 0, std::plus&lt; int &gt;(), 
            []( char prev, char next )-&gt;int { return std::isspace( prev ) &amp;&amp; std::isalnum( next )? 1: 0; } );
    }
}

int main()
{
    using namespace std;
    for( string satz; cout &lt;&lt; &quot;\nGeben Sie einen Satz ein: &quot;, getline( cin, satz ) &amp;&amp; satz != &quot;x&quot;; )
        cout &lt;&lt; &quot;Sie haben &quot; &lt;&lt; werner::wordcount( satz ) &lt;&lt; &quot; Woerter eingegeben&quot; &lt;&lt; endl;
}
</code></pre>
<p>Beispiel:</p>
<pre><code>Geben Sie einen Satz ein: Karl-Heinz treibt's um 12:00 bunt -    oder ?
Sie haben 6 Woerter eingegeben

Geben Sie einen Satz ein: Hallo Welt, Du grausame    Welt, hast Tabs und auch Zeilen sowie      Steuerzeichen
Sie haben 12 Woerter eingegeben
</code></pre>
<p>Erkennt &quot;Karl-Heinz&quot; als ein Wort; &quot;-&quot; als <strong>k</strong>ein Wort; leider auch &quot;-oder&quot; als kein Wort ... das würde mehr erfordern, als zwei hinter einander stehende Zeichen zu interpretieren.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325671</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325671</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Thu, 23 May 2013 20:46:24 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 20:57:05 GMT]]></title><description><![CDATA[<p>Phil123 schrieb:</p>
<blockquote>
<p>Aufgabe:<br />
Der Funktion wordcount wird ein String als Argument u¨bergeben. ...</p>
</blockquote>
<p>Derartige Aufgabenstellungen schränken doch bereits die Möglichkeiten der Lösung ein. Wieso eigentlich String? Irgendwie muss der Satz doch auch in den Rechner kommen, also per Input - in C++ auch als <code>std::istream</code> bekannt: zunächst mal das <code>main()</code></p>
<pre><code>#include &lt;iostream&gt;
#include &lt;locale&gt; // std::ctype&lt;&gt;

struct WordCounter
{
    WordCounter( int&amp; cnt ) : cnt_( cnt ) {}
    friend std::istream&amp; operator&gt;&gt;( std::istream&amp; in, WordCounter wc );
private:
    int&amp; cnt_;
};

int main()
{
    using namespace std;
    for( int words; cout &lt;&lt; &quot;\nGeben Sie einen Satz ein: &quot;, cin &gt;&gt; WordCounter( words ); )
        cout &lt;&lt; &quot;Sie haben &quot; &lt;&lt; words &lt;&lt; &quot; Wort(e) eingegeben&quot; &lt;&lt; endl;
}
</code></pre>
<p>fehlt noch die Implementierung des Manipulators <code>WordCounter</code> :</p>
<pre><code>std::istream&amp; operator&gt;&gt;( std::istream&amp; in, WordCounter wc )
{
    std::istream::sentry ok( in );
    if( ok )
    {
        std::ios_base::iostate state = std::ios_base::goodbit;
        try
        {
            enum Mode { Word, Space } mode = Space;
            wc.cnt_ = 0;
            typedef std::istream::traits_type Tr;
            const std::ctype&lt; char &gt;&amp; ct = std::use_facet&lt; std::ctype&lt; char &gt; &gt;( in.getloc() );
            for( char c = 0; c != '\n'; ) // bis EOL; irgendwo muss Schluss sein
            {
                const Tr::int_type m = in.rdbuf()-&gt;sbumpc();
                if( Tr::eq_int_type( m, Tr::eof() ) ) // EOF
                {
                    state |= std::ios_base::eofbit;
                    break;
                }
                c = Tr::to_char_type( m );
                if( ct.is( std::ctype_base::alnum, c ) ) // Wort
                {
                    if( mode == Space ) // Wechsel von Space zu Word -&gt; Wortanfang
                        ++wc.cnt_;
                    mode = Word;
                }
                else if( ct.is( std::ctype_base::space, c ) ) // Leerzeichen
                    mode = Space;
            }
        }
        catch( ... )
        {
            state |= std::ios_base::badbit;
            if( in.exceptions() | std::ios_base::badbit )
                throw;
        }
        in.setstate( state );
    }
    return in;
}
</code></pre>
<p>ein wenig länglich, dafür werden keine Umwege über einen String genommen (siehe auch <a href="http://www.c-plusplus.net/forum/p2256837#2256837" rel="nofollow">hier</a>).</p>
<p>Eine hübsche Aufgabe, und eine nette Übung zur Darstellung von Möglichkeiten zur Programmierung <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f576.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sunglasses"
      title=":sunglasses:"
      alt="🕶"
    /></p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325677</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325677</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Thu, 23 May 2013 20:57:05 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 20:57:10 GMT]]></title><description><![CDATA[<p>Werner Salomon schrieb:</p>
<blockquote>
<p><code>std::inner_product</code> hat noch keiner:</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>Ich werfe als Herausforderung std::set_symmetric_difference in den Raum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2325678</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325678</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 23 May 2013 20:57:10 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 21:05:40 GMT]]></title><description><![CDATA[<p>Ich wag ja kaum, eine Lösung fast ohne Hilfenahme der stl zu präsentieren, aber was sagt ihr hierzu?</p>
<pre><code class="language-cpp">template&lt;class Forward1, class Forward2&gt; inline
Forward1 find_first_notof(Forward1 first1, Forward1 last1,
						  Forward2 first2, Forward2 last2) 
{
	for (; first1 != last1; ++first1)
	{       
		Forward2 mid2;
		for (mid2 = first2; mid2 != last2; ++mid2)
			if (*first1 == *mid2)
				break;
		if(mid2==last2)
			return first1;
	}
	return first1;
}

size_t Count(const string&amp; org, const string&amp; separators)
{
	size_t c = 0;
	string::const_iterator en, be=org.begin();
	do
	{
		en=find_first_of(be, org.end(),separators.begin(), separators.end());
		if(be!=en)
			++c;
		be=find_first_notof(en, org.end(), separators.begin(), separators.end());
	} while(be!=org.end());
	return c;
}

int wordcount_fricker(string satz)
{
	return Count(satz,&quot; \n\t -;,.:?!:&quot;); // seperators hinzufügen
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2325684</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325684</guid><dc:creator><![CDATA[frickler]]></dc:creator><pubDate>Thu, 23 May 2013 21:05:40 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 21:23:54 GMT]]></title><description><![CDATA[<p>[quote=&quot;SeppJ&quot;]</p>
<p>Werner Salomon schrieb:</p>
<blockquote>
<p>Ich werfe als Herausforderung std::set_symmetric_difference in den Raum.</p>
</blockquote>
<p>Wozu? Es gibt noch genug einfache Lösungen</p>
<pre><code class="language-cpp">std::string tmp = ' '+s+' ';
return (std::unique(tmp.begin(), tmp.end(),
                    [](char c, char d){return isspace(c)!=isspace(d);})
        - tmp.begin() + 1)/2 &lt;&lt; '\n';
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2325689</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325689</guid><dc:creator><![CDATA[fummler]]></dc:creator><pubDate>Thu, 23 May 2013 21:23:54 GMT</pubDate></item><item><title><![CDATA[Reply to wordcount mit string on Thu, 23 May 2013 21:35:39 GMT]]></title><description><![CDATA[<p>Euch zuliebe hab ich die count-Lösung mal angepasst:</p>
<pre><code>int wordcount_Ethon(string satz)
{
    return count_if(satz.begin(), satz.end(), [&amp;](char&amp; c) { return &amp;c == satz.c_str() ? !isspace(c) : isalnum(c) &amp;&amp; isspace(*(&amp;c - 1)); });
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2325690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2325690</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Thu, 23 May 2013 21:35:39 GMT</pubDate></item></channel></rss>