Increment a numerical string: Difference between revisions

Content deleted Content added
Stefan (talk | contribs)
added OpenEdge solution
→‎{{header|C}}: no size limit
Line 107:
 
=={{header|C}}==
Handling strings of arbitrary sizes:<lang c>#include <stdio.h>
 
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
 
/* Constraints: input is in the form of (\+|-)?[0-9]+
void test(const char *s)
* and without leading zero (0 itself can be as "0" or "+0", but not "-0");
* input pointer is realloc'able and may change;
* if input has leading + sign, return may or may not keep it.
* The constranits conform to sprintf("%+d") and this function's own output.
*/
char * incr(char *s)
{
int i, begin, tail, len;
char rbuf[32];
int neg = int(*s == i'-');
char tgt = neg ? '0' : '9';
 
/* special case: "-1" */
/* Multiple standard functions can convert strings to integers:
if (!strcmp(s, "-1")) {
s[0] = '0', s[1] = '\0';
return 0s;
}
 
i len = atoistrlen(s);
sscanf(s, "%d", &i);
begin = (*s == '-' || i*s == (int'+')strtol(s, NULL,? 10)1 : 0;
i = atoi(s);
*/
i = atoi(s);
 
/* find out how many digits need to be changed */
/* The standard sprintf functions can convert integers to strings.
for (tail = len - 1; tail >= begin && s[tail] == tgt; tail--);
A function called itoa is also common, however it is not standard.
 
if (tail < begin && !neg) {
itoa(i+1, rbuf, 10);
/* special case: all 9s, string will grow */
*/
if (!begin) s snprintf= realloc(rbufs, 32,len "%d", i+1 2);
s[0] = '1';
for (i = 1; i <= len - begin; i++) s[i] = '0';
s[len + 1] = '\0';
} else if (tail == begin && neg && s[1] == '1') {
/* special case: -1000..., so string will shrink */
for (i = 1; i < len - begin; i++) s[i] = '9';
s[len - 1] = '\0';
} else { /* normal case; change tail to all 0 or 9, change prev digit by 1*/
for (i = len - 1; i > tail; i--)
s[i] = neg ? '9' : '0';
s[tail] += neg ? -1 : 1;
}
 
return s;
printf("\"%s\" + 1 = \"%s\"\n", s, rbuf);
}
 
void teststring_test(const char *s)
int main(void)
{
char *ret = malloc(strlen(s));
test("0");
strcpy(ret, s);
test("1");
test("-1");
test("1000000000");
test("-1000000000");
 
printf("\"%s\"text: + 1 = \"%s\"\n", s, rbufret);
return 0;
printf(" ->: %s\n", ret = incr(ret));
}</lang>
free(ret);
}
 
int main(void)
Output:
{
string_test("+0");
string_test("-1");
string_test("-41");
string_test("+41");
string_test("999");
string_test("+999");
string_test("109999999999999999999999999999999999999999");
string_test("-100000000000000000000000000000000000000000000");
 
return 0;
<pre>"0" + 1 = "1"
}</lang>output<lang>text: +0
"1" + 1 = "2"
" -1">: + 1 = "0"
text: -1
"1000000000" + 1 = "1000000001"
->: 0
"-1000000000" + 1 = "-999999999"</pre>
text: -41
->: -40
text: +41
->: +42
text: 999
->: 1000
text: +999
->: 1000
text: 109999999999999999999999999999999999999999
->: 110000000000000000000000000000000000000000
text: -100000000000000000000000000000000000000000000
->: -99999999999999999999999999999999999999999999</lang>
 
=={{header|C++}}==