<?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[Zahl ala 0001 im String inkrementieren]]></title><description><![CDATA[<p>Hey, wie der Titel schon sagt, habe ich mehrere Zahl der Form &quot;0001&quot; vorliegen.<br />
Ich möchte jetzt einem String die Zahl aus einem anderen um 1 erhöht zuweisen.</p>
<p>Bsp: 2. String ist 0013, dann soll erster String 0014 werden.</p>
<p>Vorschläge? <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>
<p>EDIT: Die Zahlen liegen nicht als richtiger String vor, sondern sind in meiner selbst geschriebenen Stringklasse mit dem Member char* pBuf.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/318685/zahl-ala-0001-im-string-inkrementieren</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Jul 2026 06:56:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/318685.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 22 Jul 2013 17:29:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 17:31:56 GMT]]></title><description><![CDATA[<p>Hey, wie der Titel schon sagt, habe ich mehrere Zahl der Form &quot;0001&quot; vorliegen.<br />
Ich möchte jetzt einem String die Zahl aus einem anderen um 1 erhöht zuweisen.</p>
<p>Bsp: 2. String ist 0013, dann soll erster String 0014 werden.</p>
<p>Vorschläge? <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>
<p>EDIT: Die Zahlen liegen nicht als richtiger String vor, sondern sind in meiner selbst geschriebenen Stringklasse mit dem Member char* pBuf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340470</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340470</guid><dc:creator><![CDATA[psp-mufflon]]></dc:creator><pubDate>Mon, 22 Jul 2013 17:31:56 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 17:31:20 GMT]]></title><description><![CDATA[<p>Nachdenken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340472</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340472</guid><dc:creator><![CDATA[vorschlagor]]></dc:creator><pubDate>Mon, 22 Jul 2013 17:31:20 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 17:34:19 GMT]]></title><description><![CDATA[<p>vorschlagor schrieb:</p>
<blockquote>
<p>Nachdenken.</p>
</blockquote>
<p>Mein Vorschlag wäre ja, den zweiten String erstmal mit atoi() in einen Int zu verwandeln und diesen dann zu erhöhen. Und wie bekomm ich die Zahl von int dann wieder in meine String-Klasse, dazu noch in der richtigen Form?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340476</guid><dc:creator><![CDATA[psp-mufflon]]></dc:creator><pubDate>Mon, 22 Jul 2013 17:34:19 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 17:38:37 GMT]]></title><description><![CDATA[<p>Kennst du diese Zählwerke, wo man eine Taste drückt und dann zählen sie eins hoch? So:<br />
<a href="http://en.wikipedia.org/wiki/File:CountersMechanical.agr.jpg" rel="nofollow">http://en.wikipedia.org/wiki/File:CountersMechanical.agr.jpg</a></p>
<p>Wie haben es die Ingenieure wohl hinbekommen, diese Wunderwerke ohne atoi und ints zu bauen? <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2340477</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340477</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 22 Jul 2013 17:38:37 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 17:45:08 GMT]]></title><description><![CDATA[<p>Also so etwas wie string1[3]=9? -&gt; string2[3]=0; string2[2]++;</p>
<p>Von der Art her? Gibts da keine leichtere Möglichkeit?<br />
Z.b. eine Art Stream, wo ich die als int umgewandelte und inkrementierte Zahl wieder in ein char* laden kann und vorher mit setfill('0') und setw(4) manipulieren kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340478</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340478</guid><dc:creator><![CDATA[psp-mufflon]]></dc:creator><pubDate>Mon, 22 Jul 2013 17:45:08 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 17:54:26 GMT]]></title><description><![CDATA[<p>psp-mufflon schrieb:</p>
<blockquote>
<p>Also so etwas wie string1[3]=9? -&gt; string2[3]=0; string2[2]++;</p>
<p>Von der Art her?</p>
</blockquote>
<p>Ja. Kann man natürlich auch in geschickt machen, anstatt mit einer Reihe Einzelprüfungen.</p>
<blockquote>
<p>Gibts da keine leichtere Möglichkeit?<br />
Z.b. eine Art Stream, wo ich die als int umgewandelte und inkrementierte Zahl wieder in ein char* laden kann und vorher mit setfill('0') und setw(4) manipulieren kann?</p>
</blockquote>
<p>Möglich wäre das, aber das kommt dir <em>leichter</em> vor? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Da fehlen als Zwischenschritte mindestens noch ausdrucken, abfotografieren, Foto einscannen, Scan durch OCR jagen, OCR-Ergebnis an einen Webservice in der Cloud schicken, Ergebnis per Briefpost als Barcode zuschicken lassen, Barcode einscannen, am Bildschirm anzeigen, mit Handy den Barcode vom Bildschirm lesen, Ergebnis an Faxservice schicken, Fax an sich selber schicken, Modem an Faxgerät anschließen, vom Modem das Endergebnis einlesen.<br />
<strong>Das</strong> wäre einfach.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340480</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340480</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 22 Jul 2013 17:54:26 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 18:23:36 GMT]]></title><description><![CDATA[<p>Das hilft mir irgendwie grad alles nicht weiter, ich steh total auf dem Schlauch^^ Ich schau mal ne Folge Dexter und denk in Ruhe drüber nach, vielleicht ist das dann bisschen verständlicher <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2340487</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340487</guid><dc:creator><![CDATA[psp-mufflon]]></dc:creator><pubDate>Mon, 22 Jul 2013 18:23:36 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 19:22:11 GMT]]></title><description><![CDATA[<p>Kaum getestet:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;cstring&gt;

int increment_digit(char* p, int carry)
{
	// Annahme: Die Ziffern 0-9 folgen im Zeichensatz aufeinander
	int digit = *p - '0' + carry;

	*p = (digit % 10) + '0';

	return digit / 10;
}

int increment_iterative(char* str)
{
	char* p = str + std::strlen(str) - 1;
	int carry = 1;

	do
	{
		carry = increment_digit(p--, carry);
	}
	while (p - str &gt;= 0 &amp;&amp; carry);

	return carry;
}

int increment_recursive(const char* str, char* p, int carry)
{
	carry = increment_digit(p--, carry);

	if (p - str &gt;= 0 &amp;&amp; carry)
		return increment_recursive(str, p, carry);
	else
		return carry;
}

int increment(char* str, bool recursive)
{
	// Annahme: Der String besteht garantiert nur aus Ziffern und ist nicht leer
	if (recursive)
		return increment_recursive(str, str + std::strlen(str) - 1, 1);
	else
		return increment_iterative(str);
}

int main()
{
	char str[] = &quot;00&quot;;

	for (int i = 0; i &lt;= 100; ++i)
	{
		std::cout &lt;&lt; &quot;Str: &quot; &lt;&lt; str;
		if (increment(str, false))
			std::cout &lt;&lt; &quot; Overflow&quot;;
		std::cout &lt;&lt; &quot;\n&quot;;
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2340496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340496</guid><dc:creator><![CDATA[GyroGearloose]]></dc:creator><pubDate>Mon, 22 Jul 2013 19:22:11 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 20:53:01 GMT]]></title><description><![CDATA[<p>Hab es jetzt so gelöst:</p>
<pre><code>char* increment(char* other)
{
    int tmp=atoi(other);
    tmp++;
    char* res=new char[strlen(&quot;0000&quot;)];
    if(tmp&lt;10) sprintf(res,&quot;000%d&quot;,tmp);
    if(tmp&lt;100)  sprintf(res,&quot;00%d&quot;,tmp);
    if(tmp&lt;1000) sprintf(res,&quot;0%d&quot;,tmp);
    else sprintf(res,&quot;%d&quot;,tmp);
    return res;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2340498</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340498</guid><dc:creator><![CDATA[psp-mufflon]]></dc:creator><pubDate>Mon, 22 Jul 2013 20:53:01 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 19:36:49 GMT]]></title><description><![CDATA[<p>GyroGearloose schrieb:</p>
<blockquote>
<p>Kaum getestet:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;cstring&gt;

int increment_digit(char* p, int carry)
{
	// Annahme: Die Ziffern 0-9 folgen im Zeichensatz aufeinander
	int digit = *p - '0' + carry;

	*p = (digit % 10) + '0';

	return digit / 10;
}

int increment_iterative(char* str)
{
	char* p = str + std::strlen(str) - 1;
	int carry = 1;

	do
	{
		carry = increment_digit(p--, carry);
	}
	while (p - str &gt;= 0 &amp;&amp; carry);

	return carry;
}

int increment_recursive(const char* str, char* p, int carry)
{
	carry = increment_digit(p--, carry);

	if (p - str &gt;= 0 &amp;&amp; carry)
		return increment_recursive(str, p, carry);
	else
		return carry;
}

int increment(char* str, bool recursive)
{
	// Annahme: Der String besteht garantiert nur aus Ziffern und ist nicht leer
	if (recursive)
		return increment_recursive(str, str + std::strlen(str) - 1, 1);
	else
		return increment_iterative(str);
}

int main()
{
	char str[] = &quot;00&quot;;

	for (int i = 0; i &lt;= 100; ++i)
	{
		std::cout &lt;&lt; &quot;Str: &quot; &lt;&lt; str;
		if (increment(str, false))
			std::cout &lt;&lt; &quot; Overflow&quot;;
		std::cout &lt;&lt; &quot;\n&quot;;
	}
}
</code></pre>
</blockquote>
<p>Oh danke, hatte gar nicht gesehen, dass du noch geschrieben hattest.. Meine Lösung ist deiner ja ähnlich, nur ein bisschen einfacher gehalten.. <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="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340499</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340499</guid><dc:creator><![CDATA[psp-mufflon]]></dc:creator><pubDate>Mon, 22 Jul 2013 19:36:49 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Mon, 22 Jul 2013 19:47:00 GMT]]></title><description><![CDATA[<p>psp-mufflon schrieb:</p>
<blockquote>
<p>Oh danke, hatte gar nicht gesehen, dass du noch geschrieben hattest.. Meine Lösung ist deiner ja ähnlich, nur ein bisschen einfacher gehalten.. <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="😃"
    /></p>
</blockquote>
<p>Nee, die sind sich überhaupt nicht ähnlich. :p Außerdem schreibt deine Lösung sonstwo in den Speicher, da dein res nicht vernünftig initialisiert ist.</p>
<p>Mein Code sieht nur soviel aus, weil ich zwei verschiedene Verfahren vorgeschlagen habe. Wenn man die iterative Lösung etwas komprimiert, sieht sie so aus:</p>
<pre><code>#include &lt;cstring&gt;
#include &lt;cctype&gt;

bool increment(char* str)
{	
	int carry = 1;

	for (char* ptr = str + std::strlen(str) - 1; carry &amp;&amp; ptr - str &gt;= 0 &amp;&amp; std::isdigit(*ptr); --ptr)
	{
		// Annahme: Die Ziffern 0-9 folgen im Zeichensatz aufeinander
		int digit = *ptr - '0' + carry;
		*ptr = (digit % 10) + '0';
		carry = digit / 10;
	}

	return carry;
}
</code></pre>
<p>Die sollte mit beliebig langen Strings, mit leeren Strings und mit Strings, die ggf. was anderes als Ziffern enthalten (bricht dann ab), klar kommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340502</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340502</guid><dc:creator><![CDATA[GyroGearloose]]></dc:creator><pubDate>Mon, 22 Jul 2013 19:47:00 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Tue, 23 Jul 2013 09:43:41 GMT]]></title><description><![CDATA[<p>psp-mufflon schrieb:</p>
<blockquote>
<p>Hab es jetzt so gelöst:</p>
<pre><code>char* increment(char* other)
{
    int tmp=atoi(other);
    tmp++;
    char* res=new char[strlen(&quot;0000&quot;)];
    if(tmp&lt;10) sprintf(res,&quot;000%d&quot;,tmp);
    if(tmp&lt;100)  sprintf(res,&quot;00%d&quot;,tmp);
    if(tmp&lt;1000) sprintf(res,&quot;0%d&quot;,tmp);
    else sprintf(res,&quot;%d&quot;,tmp);
    return res;
}
</code></pre>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
<p>Sorry, aber das ist eine recht bescheidene &quot;Lösung&quot;. So geht man nicht mit Resourcen (wie z.B. dynamisch reservierten Speicher) um. So ist die Speicherleckgefahr einfach zu hoch. Dazu hast du auch noch zuwenig Speicher reserviert. Lass diesen <code>new char[soundso]</code> -Kram ganz sein und verwende stattdessen std::string oder arbeite wenigstens in-place.</p>
<p>Du kannst doch zählen, oder? Wie geht das denn? Fällt dir da kein System auf? Du fängst bei der letzten Ziffer an. Eine Schleife. Wenn die aktuelle Ziffer kleiner als 9 ist, erhöhst du sie einfach und brichst ab (weil fertig). Wenn sie 9 ist, setzt du sie auf 0 und machst links daneben weiter. Und wenn es irgendwann kein &quot;links daneben&quot; gibt, hast du einen Überlauf, den man ignorieren oder sonst wie anders behandeln kann, je nachdem, was du willst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340579</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340579</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 23 Jul 2013 09:43:41 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl ala 0001 im String inkrementieren on Tue, 23 Jul 2013 10:13:55 GMT]]></title><description><![CDATA[<p>psp-mufflon schrieb:</p>
<blockquote>
<p>Hab es jetzt so gelöst:</p>
<pre><code>char* increment(char* other)
{
    int tmp=atoi(other);
    tmp++;
    char* res=new char[strlen(&quot;0000&quot;)];
    if(tmp&lt;10) sprintf(res,&quot;000%d&quot;,tmp);
    if(tmp&lt;100)  sprintf(res,&quot;00%d&quot;,tmp);
    if(tmp&lt;1000) sprintf(res,&quot;0%d&quot;,tmp);
    else sprintf(res,&quot;%d&quot;,tmp);
    return res;
}
</code></pre>
</blockquote>
<p>Kannst du dir nicht mal die Doku zu printf durchlesen?<br />
Das f am Ende von printf steht übrigens für formated.<br />
Und das hat seinen Grund.<br />
Ein <code>sprintf(res,&quot;%04d&quot;,tmp);</code> macht alles was du willst: 4 Stellen und führende Nullen.<br />
Eine schöne formatierte Ausgabe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2340585</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2340585</guid><dc:creator><![CDATA[DirkB]]></dc:creator><pubDate>Tue, 23 Jul 2013 10:13:55 GMT</pubDate></item></channel></rss>