Given a string containing decimal numbers:
teststring134this 123test string54 100
increment every number in this string by one to give the new string
teststring135this 124test string55 101
.
The string can be provided as:
- a command line argument
- STDIN
- a hard-coded variable or function argument
Cover all possible positions for a number:
- as a prefix for a word;
123test
►124test
- as a suffix for a word;
test123
►test124
- inside a word;
te123st
►te124st
- alone
test 123 test
►test 124 test
Here's a non-golfed solution in Python:
NUMBERS = '0123456789'
def increment(s):
out = ''
number = ''
for c in s:
if c in NUMBERS:
number += c
else:
if number != '':
out += str(int(number) + 1)
number = ''
out += c
if number != '':
out += str(int(number) + 1)
number = ''
return out
print "\"%s\"" % (increment('teststring134this 123test string54 100'))
This is a code-golf
question, shortest code wins.
NUMBERS
as a global variable insideincrement(s)
or is it different in Python 2? – Beta Decay 2 hours ago