Increment a numerical string: Difference between revisions

Tag: Manual revert
 
(9 intermediate revisions by 7 users not shown)
Line 8:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V next = String(Int(‘123’) + 1)</syntaxhighlight>
 
 
=={{header|8080 Assembly}}==
Line 70 ⟶ 68:
 
=={{header|8086 Assembly}}==
 
<syntaxhighlight lang="asm"> cpu 8086
bits 16
Line 392 ⟶ 389:
 
=={{header|APL}}==
 
<syntaxhighlight lang="apl">⍕1+⍎'12345'</syntaxhighlight>
{{Out}}
Line 800 ⟶ 796:
.align 4
.Ls_magic_number_10: .word 0x66666667
 
</syntaxhighlight>
Line 865 ⟶ 860:
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="gwbasic">S$ = "12345":S$ = STR$ ( VAL (S$) + 1)</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">cadena$ = "12345"
Line 895 ⟶ 891:
 
==={{header|ZX Spectrum Basic}}===
 
The ZX Spectrum needs line numbers and a let statement,
but the same technique can be used:
 
<syntaxhighlight lang="zxbasic">10 LET s$ = "12345"
20 LET s$ = STR$(VAL(s$) + 1)</syntaxhighlight>
Line 938 ⟶ 932:
 
=={{header|Bracmat}}==
Numbers are strings. Bracmat supports rational numbers, including integers, using arbitrary-precision arithmetic. Pure imaginary numbers are formed using a factor <code>i</code> (or <code>-i</code>). There is no support for floating point arithmetics. (Historically, because the ARM 2 processor in the Archimedes computer didn't sport an FPU.)
<syntaxhighlight lang="bracmat">(n=35664871829866234762187538073934873121878/6172839450617283945)
&!n+1:?n
Line 945 ⟶ 939:
35664871829866234762193710913385490405823/6172839450617283945
</syntaxhighlight>
Output
<pre></pre>
 
Bracmat also supports floating point numbers, but only in a sandboxed way. To increment 0.234E0 by 1:
 
<syntaxhighlight lang="bracmat">(new$(UFP,(=(s.val).!val+1)).go)$"0.234e0"</syntaxhighlight>
Output
<pre>1.2340000000000000E+00</pre>
 
<code>UFP</code> is a new (2023) object type that specializes in compiling and executing floating point operations. Bracmat code must be passed when a UFP object is created. This code has a more restricted grammar than Bracmat code in general. The passed code must be the definition of a function that can take one or more scalars or arrays as argument. In the example, this function is <code>(=(s.val).!val+1)</code>. The expression <code>(s.val)</code> says that the function takes a scalar and binds it to the local variable <code>val</code>. In the example, the function is defined, compiled, executed and destroyed in one go. This is not necessary; it is perfectly possible to use the compiled UFP object multiple times before destroying it.
 
=={{header|Brat}}==
Line 951 ⟶ 955:
 
=={{header|Burlesque}}==
 
<syntaxhighlight lang="burlesque">
ri?ish
Line 1,248 ⟶ 1,251:
assert(s == "12350");
}</syntaxhighlight>
 
=={{header|dc}}==
<syntaxhighlight lang="dc">? 1 + p</syntaxhighlight>
The program expects a string on ''stdin'':
<syntaxhighlight lang="sh">echo '12345678899' | dc inc.dc</syntaxhighlight>
{{out}}
<pre>12345678900</pre>
 
=={{header|Delphi}}==
Line 1,266 ⟶ 1,276:
{{Out}}
<pre>"12345" + 1 = 123456</pre>
 
=={{header|dt}}==
<syntaxhighlight lang="dt">"1234567889" to-int 1 + to-string</syntaxhighlight>
 
=={{header|Dyalect}}==
Line 1,396 ⟶ 1,409:
 
=={{header|Emacs Lisp}}==
 
<syntaxhighlight lang="lisp">(1+ (string-to-number "12345"))</syntaxhighlight>
 
Line 1,421 ⟶ 1,433:
 
=={{header|Erlang}}==
 
<syntaxhighlight lang="erlang">integer_to_list(list_to_integer("1336")+1).</syntaxhighlight>
 
Line 1,461 ⟶ 1,472:
 
=={{header|Fantom}}==
 
Within 'fansh':
 
Line 1,589 ⟶ 1,599:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Increment_a_numerical_string}}
 
'''Solution'''
 
[[File:Fōrmulæ - Increment a numerical string 01.png]]
 
[[File:Fōrmulæ - Increment a numerical string 02.png]]
 
=={{header|Gambas}}==
Line 1,984 ⟶ 2,000:
<pre>42 1492.3 -0.5 137
42 pine martens in 1492.3 -0.5 mushrooms ≠ 137</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="jq">DEFINE incstr == 10 strtol succ 'd 1 1 format.
 
$s++;"1234567889" incstr.</syntaxhighlight>
{{out}}
<pre>"1234567890"</pre>
 
=={{header|jq}}==
Line 2,094 ⟶ 2,117:
 
=={{header|Lambdatalk}}==
 
<syntaxhighlight lang="scheme">
In lambdatalk every expression is a word or an S-expression, so:
Line 2,174 ⟶ 2,196:
 
=={{header|LOLCODE}}==
 
LOLCODE is weakly typed, so the arithmetic operators work "as expected" on strings.
 
Line 2,272 ⟶ 2,293:
numStr = num2str(str2double(numStr) + 1);
end</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
inc_string(str):=if stringp(str) and numberp(eval_string(str)) then string(eval_string(str)+1)$
"12345"$
inc_string(%);
</syntaxhighlight>
{{out}}
<pre>
"12346"
</pre>
 
=={{header|MAXScript}}==
Line 2,517 ⟶ 2,549:
 
=={{header|Octave}}==
 
We convert the string to a number, increment it, and convert it back to a string.
 
Line 2,821 ⟶ 2,852:
 
=={{header|plainTeX}}==
 
<syntaxhighlight lang="tex">\newcount\acounter
\def\stringinc#1{\acounter=#1\relax%
Line 2,911 ⟶ 2,941:
 
=={{header|Quackery}}==
 
As a dialogue in the Quackery shell.
 
Line 2,947 ⟶ 2,976:
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<syntaxhighlight lang="raku" line>say my $s = "12345";
say ++$s;
$s++;</syntaxhighlight>
 
# or Unicode. How about Sinhala?
 
say "෧෨෩෪෫ ", +"෧෨෩෪෫";
say "෧෨෩෪෫".succ, ' ', +"෧෨෩෪෫".succ;</syntaxhighlight>
{{out}}
<pre>12345
12346
෧෨෩෪෫ 12345
෧෨෩෪෬ 12346</pre>
 
=={{header|Rascal}}==
Line 2,993 ⟶ 3,031:
 
=={{header|Retro}}==
 
<syntaxhighlight lang="retro">'123 s:to-number n:inc n:to-string</syntaxhighlight>
 
=={{header|REXX}}==
 
REXX, like many other scripting languages, uses typeless variables.
<br>Typeless variables are stored as variable length character strings and can be treated as
Line 3,316 ⟶ 3,352:
 
=={{header|TXR}}==
 
Two implementations of what the task says: incrementing a numerical string. (Not: converting a string to a number, then incrementing the number, then converting back to string.)
 
Line 3,417 ⟶ 3,452:
 
=={{header|Ursala}}==
 
<syntaxhighlight lang="ursala">#import nat
 
Line 3,588 ⟶ 3,622:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var ns = "41"
var n = Num.fromString(ns) + 1
var ns2 = "%(n)"
305

edits