Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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; 123test124test
  • as a suffix for a word; test123test124
  • inside a word; te123stte124st
  • alone test 123 testtest 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.

share|improve this question
2  
Fun fact: this can be done with 3 pure regex substitutions (no callbacks) stackoverflow.com/questions/12941362/… (that wouldn't be the golfiest way though) –  Martin Büttner 5 hours ago
4  
You specified input but not output. From your input spec I assume both STDOUT and and return value are fine. But can we also store the result in a hardcoded variable (just as we can take input from it)? –  Martin Büttner 5 hours ago
    
Your code shouldn't work... Don't you need to declare NUMBERS as a global variable inside increment(s) or is it different in Python 2? –  Beta Decay 2 hours ago

9 Answers 9

Perl, 23

Assumes the input string is assigned to $_

s/\d+/@{[$&+1]}/g;print
share|improve this answer

Perl, 14 bytes

s/\d+/$&+1/ge

Requires the -p switch, which I have counted as one byte.

Example run

$ perl -p <(echo 's/\d+/$&+1/ge') <<< 'teststring134this 123test string54 100'
teststring135this 124test string55 101
share|improve this answer

Python 2 - 59

Supply the string as the variable n

import re
print re.sub('\d+',lambda x:`int(x.group())+1`,n)
share|improve this answer

J (20)

Expects the input to be stored in the variable a.

'\d+'>:&.".rxapply a

Test:

   a=:'teststring134this 123test string54 100'
   '\d+'>:&.".rxapply a
teststring135this 124test string55 101
share|improve this answer

Ruby, 30 24 bytes

$><<s.gsub(/\d+/,&:next)

Expects the input to be stored in s.

share|improve this answer
1  
Couldn't $1.next be used in the block? –  August 4 hours ago
    
@August nice, thanks! I didn't know next was that sophisticated. –  Martin Büttner 4 hours ago

JavaScript (ES6) - 28

H=k=>k.replace(/\d+/g,k=>++k)

Run by using H("test 123 234t").

share|improve this answer

C 191 (if line endings count as 1)

This will never win but it is in a language that doesn't lend itself to golfing...

This is using a hard coded string but the contents of the string are not counted towards the total.

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
i;main(){char o[99],*p=o,*q,*s="test123test999test-1test";for(;*s;*p=0){i=strtol(s,&q,0);if(q>s){sprintf(p,"%d",i+1);p+=strlen(p);s=q;}else*p++=*s++;}puts(o);}

Basically it runs through the string one character at a time checking each to see if it is an integer. If it is then it increments the integer and writes it to the output otherwise it copies the current character to the output.

This leaks the hardcoded string because it increments s.

share|improve this answer
1  
I'm sure you can get rid of some of the includes and it will still compile fine with gcc. –  Martin Büttner 4 hours ago
    
Will this work with a string containing e.g. 99? –  anatolyg 23 mins ago

Lua - 68 characters

d='(%D-)'for k,i,j in s:gmatch(d..'(%d+)'..d)do io.write(k,i+1,j)end

Expects the input to be stored in s.

share|improve this answer

Emacs - 20 characters

C-M-% [0-9]+ RET \,(1+ \#0) RET !

Requires text to be processed to be present in the current buffer. I counted C-M-% as one character here since it can be entered with one keystroke when holding down three modifiers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.