Increment a numerical string: Difference between revisions
Content deleted Content added
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;
int neg =
char tgt = neg ? '0' : '9';
/* special case: "-1" */
if (!strcmp(s, "-1")) {
s[0] = '0', s[1] = '\0';
}
begin = (*s == '-' ||
▲ i = atoi(s);
/* find out how many digits need to be changed */
for (tail = len - 1; tail >= begin && s[tail] == tgt; tail--);
if (tail < begin && !neg) {
/* special case: all 9s, string will grow */
if (!begin) s
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);▼
}
int main(void)▼
{
char *ret = malloc(strlen(s));
strcpy(ret, s);
▲ return 0;
printf(" ->: %s\n", ret = incr(ret));
free(ret);
}
{
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;
}</lang>output<lang>text: +0
text: -1
->: 0
text: -41
->: -40
text: +41
->: +42
text: 999
->: 1000
text: +999
->: 1000
text: 109999999999999999999999999999999999999999
->: 110000000000000000000000000000000000000000
text: -100000000000000000000000000000000000000000000
->: -99999999999999999999999999999999999999999999</lang>
=={{header|C++}}==
|