Increment a numerical string: Difference between revisions

m (Move 'Free Pascal' as subtype of Pascal)
Tag: Manual revert
 
(41 intermediate revisions by 23 users not shown)
Line 8:
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V next = String(Int(‘123’) + 1)</syntaxhighlight>
 
<lang 11l>V next = String(Int(‘123’) + 1)</lang>
 
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="8080asm"> org 100h
jmp demo
;;; Increment the number in the $-terminated string under HL.
Line 67 ⟶ 65:
pop d ; Print the string
mvi c,9
jmp 5</langsyntaxhighlight>
 
=={{header|8086 Assembly}}==
<syntaxhighlight lang="asm"> cpu 8086
 
<lang asm> cpu 8086
bits 16
section .text
Line 121 ⟶ 118:
mov ah,9 ; Print the string
int 21h
ret</langsyntaxhighlight>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program incstring64.s */
Line 194 ⟶ 191:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 207 ⟶ 204:
 
=={{header|ABAP}}==
<langsyntaxhighlight ABAPlang="abap">report zz_incstring
perform test using: '0', '1', '-1', '10000000', '-10000000'.
 
Line 218 ⟶ 215:
write / lv_string.
endform.
</syntaxhighlight>
</lang>
 
{{Out}}
Line 230 ⟶ 227:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Increment(CHAR ARRAY src,dst)
INT val
 
Line 251 ⟶ 248:
Test("-2")
Test("-10000")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Increment_a_numerical_string.png Screenshot from Atari 8-bit computer]
Line 264 ⟶ 261:
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">function incrementString(str:String):String
{
return String(Number(str)+1);
}</langsyntaxhighlight>
 
=={{header|Ada}}==
The standard Ada package Ada.Strings.Fixed provides a function for trimming blanks from a string.
<langsyntaxhighlight lang="ada">S : String := "12345";
S := Ada.Strings.Fixed.Trim(Source => Integer'Image(Integer'Value(S) + 1), Side => Ada.Strings.Both);</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">
o_text(itoa(atoi("2047") + 1));
o_byte('\n');
</syntaxhighlight>
</lang>
 
=={{header|ALGOL 68}}==
Line 285 ⟶ 282:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<langsyntaxhighlight lang="algol68">STRING s := "12345"; FILE f; INT i;
associate(f, s); get(f,i);
i+:=1;
s:=""; reset(f); put(f,i);
print((s, new line))</langsyntaxhighlight>
{{Out}}
<pre>
Line 297 ⟶ 294:
=={{header|ALGOL W}}==
Increments a string representaton of an integer, without converting it to an integer and so allows values greater than will fit into an Algol W integer (which is restricted to 32 bits).
<langsyntaxhighlight lang="algolw">begin
% returns a string representing the number in s incremented %
% As strings are fixed length, the significant length of s must %
Line 373 ⟶ 370:
write( " 123456789 + 1: " ); writeonToBlank( increment( "123456789", 9 ) );
write( "99999999999999999999 + 1: " ); writeonToBlank( increment( "99999999999999999999", 20 ) )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 383 ⟶ 380:
 
=={{header|Apex}}==
<langsyntaxhighlight lang="apex">
string count = '12345';
count = String.valueOf(integer.valueOf(count)+1);
system.debug('Incremental Value : '+count);
</syntaxhighlight>
</lang>
{{Out}}
<pre>12346</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">⍕1+⍎'12345'</syntaxhighlight>
 
<lang apl>⍕1+⍎'12345'</lang>
{{Out}}
<pre>12346</pre>
 
=={{header|AppleScript}}==
===Functional===
Preserving the distinction between real and integer strings, and allowing for strings containing non-numeric tokens and/or multiple numeric expressions. Provides an option to either retain or prune out any non-numeric tokens in the string:
{{Trans|Python}}
{{Trans|Haskell}}
{{Trans|JavaScript}}
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 508 ⟶ 505:
filteredArrayUsingPredicate:(ca's ¬
NSPredicate's predicateWithFormat:"0 < length")) as list
end |words|</langsyntaxhighlight>
{{Out}}
<pre>42 1492.3 -0.5 137
42 pine martens in 1492.3 -0.5 mushrooms ≠ 137</pre>
----
===AppleScriptObjC===
The task description's delightfully unforthcoming about what it means by "increment" ("add 1" as in C-related languages or "add a certain amount" as in English?) and what "numerical string" covers with respect to size, number type, format, locale, and base. At its most trivial, given a string representing a modest, unformatted decimal integer, the vanilla AppleScript solution would be:
 
<syntaxhighlight lang="applescript">("12345" + 1) as text --> "12346"</syntaxhighlight>
 
The following handles more possibilities, but the number base is still resolutely decimal:
 
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
-- Using the machine's own locale setting. The numerical text must be compatible with this.
-- Params: Numerical text or NSString, AppleScript or Objective-C number or text equivalent.
on incrementNumericalString:str byAmount:increment
set localeID to current application's class "NSLocale"'s currentLocale()'s localeIdentifier()
return my incrementNumericalString:str byAmount:increment localeID:localeID
end incrementNumericalString:byAmount:
 
-- Including the locale ID as an additional parameter.
-- Params: As above plus locale ID (text or NSString).
on incrementNumericalString:str byAmount:increment localeID:localeID
set |⌘| to current application
set str to |⌘|'s class "NSString"'s stringWithString:(str)
set locale to |⌘|'s class "NSLocale"'s localeWithLocaleIdentifier:(localeID)
set decSeparator to locale's objectForKey:(|⌘|'s NSLocaleDecimalSeparator)
set regex to |⌘|'s NSRegularExpressionSearch
-- Use an NSNumberFormatter to generate the NSDecimalNumber objects for the math,
-- as its number/string conversions are more flexible than NSDecimalNumber's own.
tell |⌘|'s class "NSNumberFormatter"'s new()
its setGeneratesDecimalNumbers:(true)
its setLocale:(locale)
set symbolRange to str's rangeOfString:("[Ee]| ?[x*] ?10 ?\\^ ?") options:(regex)
if (symbolRange's |length|() > 0) then
-- Catered-for exponent symbol in the input string. Set the output style to "scientific".
its setNumberStyle:(|⌘|'s NSNumberFormatterScientificStyle)
its setExponentSymbol:(str's substringWithRange:(symbolRange))
else
-- Straight numerical text, with or without separators as per the input and locale.
its setNumberStyle:(|⌘|'s NSNumberFormatterDecimalStyle)
set groupingSeparator to locale's objectForKey:(|⌘|'s NSLocaleGroupingSeparator)
its setUsesGroupingSeparator:(str's containsString:(groupingSeparator))
its setMinimumFractionDigits:(str's containsString:(decSeparator))
end if
-- Derive NSDecimalNumbers from the inputs, add together, convert the result back to NSString.
set increment to (|⌘|'s class "NSArray"'s arrayWithArray:({increment}))'s firstObject()
if ((increment's isKindOfClass:(|⌘|'s class "NSNumber")) as boolean) then ¬
set increment to its stringFromNumber:(increment)
set sum to (its numberFromString:(str))'s decimalNumberByAdding:(its numberFromString:(increment))
set output to its stringFromNumber:(sum)
end tell
-- Adjustments for AppleScript norms the NSNumberFormatter may omit from scientific notation output:
if (symbolRange's |length|() > 0) then
-- If no decimal separator in the output mantissa, insert point zero or not to match the input style.
if ((output's containsString:(decSeparator)) as boolean) then
else if ((str's containsString:(decSeparator)) as boolean) then
set output to output's stringByReplacingOccurrencesOfString:("(?<=^-?\\d)") ¬
withString:((decSeparator as text) & "0") options:(regex) range:({0, output's |length|()})
end if
-- If no sign in an E-notation exponent, insert "+" or not ditto.
if (((output's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
else if (((str's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
set output to output's stringByReplacingOccurrencesOfString:("(?<=[Ee])") ¬
withString:("+") options:(regex) range:({0, output's |length|()})
end if
end if
return output as text -- Return as AppleScript text.
end incrementNumericalString:byAmount:localeID:
 
return {¬
(my incrementNumericalString:"12345" byAmount:1), ¬
(my incrementNumericalString:"999,999,999,999,999" byAmount:5), ¬
(my incrementNumericalString:"-1.234" byAmount:10 localeID:"en"), ¬
(my incrementNumericalString:"-1,234E+1" byAmount:10 localeID:"fr_FR"), ¬
(my incrementNumericalString:"-1.234 x 10^1" byAmount:"10") ¬
}</syntaxhighlight>
 
{{output}}
(On a machine configured for "en_GB".)
<syntaxhighlight lang="applescript">{"12346", "1,000,000,000,000,004", "8.766", "-2,34E+0", "-2.34 x 10^0"}</syntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 719 ⟶ 796:
.align 4
.Ls_magic_number_10: .word 0x66666667
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">num: "12349"
 
print ["The next number is:" (to :integer num)+1]</langsyntaxhighlight>
 
{{out}}
Line 733 ⟶ 809:
 
=={{header|Asymptote}}==
<langsyntaxhighlight Asymptotelang="asymptote">string cadena = "12345.78";
cadena = string((real)cadena + 1);
write(cadena);</langsyntaxhighlight>
{{out}}
<pre>12346.78</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">str = 12345
MsgBox % str += 1</langsyntaxhighlight>
{{Out}}
<pre>12346</pre>
 
=={{header|AutoIt}}==
<langsyntaxhighlight autoItlang="autoit">Global $x = "12345"
$x += 1
MsgBox(0,"",$x)</langsyntaxhighlight>
{{Out}}
<pre>12346</pre>
 
=={{header|Avail}}==
<langsyntaxhighlight Availlang="avail">numberString ::= "1080";
incremented ::= numberString (base 10) + 1;</langsyntaxhighlight>
 
=={{header|AWK}}==
The example shows that the string ''s'' can be incremented,
but after that still is a string of length 2.
<langsyntaxhighlight lang="awk">$ awk 'BEGIN{s="42"; s++; print s"("length(s)")" }'
43(2)</langsyntaxhighlight>
 
=={{header|BASIC}}==
{{works with|Applesoft BASIC}}
 
{{works with|BaCon}}
 
Line 777 ⟶ 855:
{{works with|Yabasic}}
 
<langsyntaxhighlight lang="qbasic">s$ = "12345"
s$ = STR$(VAL(s$) + 1)</langsyntaxhighlight>
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="gwbasic">S$ = "12345":S$ = STR$ ( VAL (S$) + 1)</syntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="freebasic">cadena$ = "12345"
cadena$ = string(val(cadena$) + 1)
#or also
cadena$ = string(FromRadix(cadena$,10) + 1)</langsyntaxhighlight>
 
==={{header|OxygenBasic}}===
When a number is assigned to a string, it autoconverts.
<syntaxhighlight lang="text">
string s="122"
s=val(s)+1
print s 'result: "123"
</syntaxhighlight>
</lang>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<langsyntaxhighlight lang="qbasic">LET cadena$ = "12345"
LET cadena$ = STR$(VAL(cadena$) + 1)
PRINT cadena$
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">cadena$ = "12345"
cadena$ = str$(val(cadena$) + 1)</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 LET S$="12345"
110 LET S$=STR$(VAL(S$)+1)</langsyntaxhighlight>
 
==={{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>
<lang zxbasic>10 LET s$ = "12345"
20 LET s$ = STR$(VAL(s$) + 1)</lang>
 
=={{header|Batch File}}==
Line 821 ⟶ 900:
 
{{works with|Windows NT|4}}
<langsyntaxhighlight lang="dos">set s=12345
set /a s+=1</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
This assumes the task is about incrementing an ''arbitrary-length'' decimal string.
<langsyntaxhighlight lang="bbcbasic"> num$ = "567"
REPEAT
PRINT num$
Line 843 ⟶ 922:
UNTIL A$<>"0" OR I%=0
IF A$="0" n$ = "1" + n$
ENDPROC</langsyntaxhighlight>
 
=={{header|Boo}}==
<langsyntaxhighlight lang="boo">s = "1234"
s = (int.Parse(s) + 1).ToString()</langsyntaxhighlight>
 
=={{header|BQN}}==
<langsyntaxhighlight BQNlang="bqn">1•Repr∘+•BQN "1234"</langsyntaxhighlight>
 
=={{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.)
<langsyntaxhighlight lang="bracmat">(n=35664871829866234762187538073934873121878/6172839450617283945)
&!n+1:?n
&out$!n
 
35664871829866234762193710913385490405823/6172839450617283945
</syntaxhighlight>
</lang>
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}}==
<langsyntaxhighlight lang="brat">#Convert to integer, increment, then back to string
p ("100".to_i + 1).to_s #Prints 101</langsyntaxhighlight>
 
=={{header|Burlesque}}==
<syntaxhighlight lang="burlesque">
 
<lang burlesque>
ri?ish
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Handling strings of arbitrary sizes:<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Line 941 ⟶ 1,029:
 
return 0;
}</langsyntaxhighlight>
{{Out}}<pre>
text: +0
Line 961 ⟶ 1,049:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">string s = "12345";
s = (int.Parse(s) + 1).ToString();
// The above functions properly for strings >= Int32.MinValue and
Line 973 ⟶ 1,061:
bis = (BigInteger.Parse(bis) + 1).ToString();
// Note that extremely long strings will take a long time to parse
// and convert from a BigInteger back into a string.</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">// standard C++ string stream operators
#include <cstdlib>
#include <string>
Line 991 ⟶ 1,079:
 
std::ostringstream oss;
if (oss << i) s = oss.str();</langsyntaxhighlight>
 
{{works with|C++11}}
<langsyntaxhighlight lang="cpp">#include <string>
 
std::string s = "12345";
s = std::to_string(1+std::stoi(s));</langsyntaxhighlight>
 
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">// Boost
#include <cstdlib>
#include <string>
Line 1,008 ⟶ 1,096:
std::string s = "12345";
int i = boost::lexical_cast<int>(s) + 1;
s = boost::lexical_cast<std::string>(i);</langsyntaxhighlight>
 
{{libheader|Qt}}
{{uses from|Library|Qt|component1=QString}}
<langsyntaxhighlight lang="cpp">// Qt
QString num1 = "12345";
QString num2 = QString("%1").arg(v1.toInt()+1);</langsyntaxhighlight>
 
{{Libheader|MFC}}
Line 1,020 ⟶ 1,108:
{{uses from|Library|C Runtime|component1=_ttoi|component2=_tcstoul}} <!-- They're Microsoft-specific extensions exported in tchar.h. That'll be noted on their relevant pages.-->
 
<langsyntaxhighlight lang="cpp">// MFC
CString s = "12345";
int i = _ttoi(s) + 1;
int i = _tcstoul(s, NULL, 10) + 1;
s.Format("%d", i);</langsyntaxhighlight>
 
All of the above solutions only work for numbers <= INT_MAX. The following works for an (almost) arbitrary large number:
 
{{works with|g++|4.0.2}}
<langsyntaxhighlight lang="cpp">#include <string>
#include <iostream>
#include <ostream>
Line 1,054 ⟶ 1,142:
increment_numerical_string(big_number);
std::cout << "after increment: " << big_number << "\n";
}</langsyntaxhighlight>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
"Increments a numeric string by 1. Returns a float or integer depending on the string.
Line 1,074 ⟶ 1,162:
value d = inc(c);
print(d);
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(str (inc (Integer/parseInt "1234")))</langsyntaxhighlight>
 
=={{header|CMake}}==
CMake performs all arithmetic with numeric strings, through its math() command.
 
<langsyntaxhighlight lang="cmake">set(string "1599")
math(EXPR string "${string} + 1")
message(STATUS "${string}")</langsyntaxhighlight>
 
<pre>-- 1600</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> PROGRAM-ID. increment-num-str.
DATA DIVISION.
Line 1,102 ⟶ 1,190:
 
GOBACK
.</langsyntaxhighlight>
 
The following example also increments a numerical string, although it does not apear to. num-str is implicitly defined as <code>USAGE DISPLAY</code> which means its contents will be stored as characters. This means num-str is effectively a string of (numeric) characters.
<langsyntaxhighlight lang="cobol"> PROGRAM-ID. increment-num-str.
DATA DIVISION.
Line 1,117 ⟶ 1,205:
GOBACK
.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(princ-to-string (1+ (parse-integer "1234")))</langsyntaxhighlight>
 
=={{header|Component Pascal}}==
BlackBox Component Builder
<langsyntaxhighlight lang="oberon2">
MODULE Operations;
IMPORT StdLog,Args,Strings;
Line 1,149 ⟶ 1,237:
 
END Operations.
</syntaxhighlight>
</lang>
Execute: ^Q Operatiosn.DoIncString 124343~<br/>
{{Out}}
Line 1,157 ⟶ 1,245:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.string;
 
immutable s = "12349".succ;
assert(s == "12350");
}</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight Delphilang="delphi">program IncrementNumericalString;
 
{$APPTYPE CONSOLE}
Line 1,177 ⟶ 1,272:
 
Readln;
end.</langsyntaxhighlight>
 
{{Out}}
<pre>"12345" + 1 = 123456</pre>
 
=={{header|dt}}==
<syntaxhighlight lang="dt">"1234567889" to-int 1 + to-string</syntaxhighlight>
 
=={{header|Dyalect}}==
<langsyntaxhighlight lang="dyalect">var str = "42"
str = (parse(str) + 1).ToString()
 
Line 1,192 ⟶ 1,290:
str = "\(parse(str) + 1)"
 
print(str) //Outputs 45</langsyntaxhighlight>
 
=={{header|DWScript}}==
<langsyntaxhighlight Delphilang="delphi">var value : String = "1234";
value := IntToStr(StrToInt(value) + 1);
PrintLn(value);</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu"> !. to-str ++ to-num "100"</langsyntaxhighlight>
 
{{out}}
Line 1,206 ⟶ 1,304:
 
=={{header|E}}==
<langsyntaxhighlight lang="e">__makeInt("1234", 10).next().toString(10)</langsyntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">a$ = "12"
a$ = number a$ + 1
print a$</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(number->string (1+ (string->number "665")))
→ "666"
</syntaxhighlight>
</lang>
 
=={{header|Ecstasy}}==
Ecstasy provides two types that represent various numbers as if they were String values: IntLiteral and FPLiteral. For example:
<syntaxhighlight lang="java">String s = "12345";
IntLiteral lit1 = new IntLiteral(s);
IntLiteral lit2 = 6789;
++lit1; // lit1=12346
++lit2; // lit2=6790</syntaxhighlight>
 
=={{header|Eero}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main()
Line 1,229 ⟶ 1,335:
 
Log( '%@', s )
return 0</langsyntaxhighlight>
 
=={{header|EGL}}==
<langsyntaxhighlight EGLlang="egl">s string = "12345";
s = 1 + s; // Note: s + 1 is a string concatenation.</langsyntaxhighlight>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,259 ⟶ 1,365:
 
end
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,269 ⟶ 1,375:
=={{header|Elena}}==
ELENA 4.x:
<langsyntaxhighlight lang="elena">import extensions;
public program()
Line 1,277 ⟶ 1,383:
console.printLine(s)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,285 ⟶ 1,391:
=={{header|Elixir}}==
Values can be converted to integers then converted back after incrementing
<langsyntaxhighlight Elixirlang="elixir">increment1 = fn n -> to_string(String.to_integer(n) + 1) end
# Or piped
increment2 = fn n -> n |> String.to_integer |> +1 |> to_string end
 
increment1.("1")
increment2.("100")</langsyntaxhighlight>
{{out}}
<pre>
Line 1,299 ⟶ 1,405:
 
'''Case of char list:'''
<langsyntaxhighlight Elixirlang="elixir">iex(1)> (List.to_integer('12345') + 1) |> to_char_list
'12346'</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">(1+ (string-to-number "12345"))</syntaxhighlight>
 
=={{header|EMal}}==
<lang lisp>(1+ (string-to-number "12345"))</lang>
<syntaxhighlight lang="emal">
fun incrementGeneric = text by generic T, text numerical
return text!(:T!numerical + 1)
end
fun increment = text by text numerical
return incrementGeneric(when(numerical.contains("."), real, int), numerical)
end
writeLine(incrementGeneric(real, "123.32"))
writeLine(incrementGeneric(int, "123"))
writeLine(increment("123.32"))
writeLine(increment("123"))
</syntaxhighlight>
{{out}}
<pre>
124.32
124
124.32
124
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">integer_to_list(list_to_integer("1336")+1).</syntaxhighlight>
 
<lang erlang>integer_to_list(list_to_integer("1336")+1).</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
....................
s$="12345"
s$=STR$(VAL(s$)+1)
....................
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include get.e
 
function val(sequence s)
Line 1,330 ⟶ 1,455:
 
s = "12345"
s = sprintf("%d",{val(s)+1})</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
<lang fsharp>let next = string( int( "1234" ) + 1 )</lang>
// Increment a numerical string. Nigel Galloway: April 4th., 2023
let inc=int>>(+)1>>string
printfn "%s" (inc("1234"))
</syntaxhighlight>
{{out}}
<pre>
1235
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">"1234" string>number 1 + number>string</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
Within 'fansh':
 
<langsyntaxhighlight lang="fantom">
fansh> a := "123"
123
fansh> (a.toInt + 1).toStr
124
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
Line 1,354 ⟶ 1,486:
The word ">string" takes and integer and returns the string representation of that integer. I factored it out of the definitions below to keep the example simpler.
 
<langsyntaxhighlight lang="forth">: >string ( d -- addr n )
dup >r dabs <# #s r> sign #> ;
 
: inc-string ( addr -- )
dup count number? not abort" invalid number"
1 s>d d+ >string rot place ;</langsyntaxhighlight>
 
Here is a version that can increment by any value
 
<langsyntaxhighlight lang="forth">: inc-string ( addr n -- )
over count number? not abort" invalid number"
rot s>d d+ >string rot place ;</langsyntaxhighlight>
 
Test the first version like this:
 
<langsyntaxhighlight lang="forth">s" 123" pad place
pad inc-string
pad count type</langsyntaxhighlight>
 
And the second one like this:
 
<langsyntaxhighlight lang="forth">s" 123" pad place
pad 1 inc-string
pad count type</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
Using 'internal' files you can increment both integer and real strings
<langsyntaxhighlight lang="fortran">CHARACTER(10) :: intstr = "12345", realstr = "1234.5"
INTEGER :: i
REAL :: r
Line 1,392 ⟶ 1,524:
READ(realstr, "(F10.1)") r
r = r + 1.0
WRITE(realstr, "(F10.1)") r</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function Increment (num As String) As String
Line 1,409 ⟶ 1,541:
Print
Print "Press any key to exit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,423 ⟶ 1,555:
=={{header|Frink}}==
The following works for integers, rational numbers, complex numbers, floating-point, etc.
<syntaxhighlight lang="frink">a = input["Enter number: "]
<lang frink>
toString[eval[a] + 1]</syntaxhighlight>
a = input["Enter number: "]
 
toString[eval[a] + 1]
=={{header|FTCBASIC}}==
</lang>
<syntaxhighlight lang="basic">define value = 0, text$ = "12345"
 
strint value,text$
+1 value
intstr text$,value
 
print text$
pause
end</syntaxhighlight>
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
Line 1,439 ⟶ 1,580:
next
HandleEvents
</syntaxhighlight>
</lang>
 
Output:
Line 1,457 ⟶ 1,598:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Increment_a_numerical_string}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
[[File:Fōrmulæ - Increment a numerical string 01.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Increment a numerical string 02.png]]
In '''[https://formulae.org/?example=Increment_a_numerical_string this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=e31b63f444c2c09c8b1436407020e216 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim vInput As Variant = "12345"
 
Line 1,471 ⟶ 1,614:
Print vInput
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,478 ⟶ 1,621:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap"># Using built-in functions
Incr := s -> String(Int(s) + 1);
 
Line 1,507 ⟶ 1,650:
Increment(s);
s;
# "2400"</langsyntaxhighlight>
 
=={{header|Go}}==
Concise:
<langsyntaxhighlight lang="go">package main
import "fmt"
import "strconv"
Line 1,517 ⟶ 1,660:
i, _ := strconv.Atoi("1234")
fmt.Println(strconv.Itoa(i + 1))
}</langsyntaxhighlight>
More:
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,596 ⟶ 1,739:
bs = b.Add(&b, &one).String()
fmt.Println("incremented:", bs)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,619 ⟶ 1,762:
 
=={{header|Golfscript}}==
<syntaxhighlight lang ="golfscript">~)`</langsyntaxhighlight>
With a test framework to supply a number:
<langsyntaxhighlight lang="golfscript">"1234" ~)` p</langsyntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">println ((("23455" as BigDecimal) + 1) as String)
println ((("23455.78" as BigDecimal) + 1) as String)</langsyntaxhighlight>
 
{{Out}}
Line 1,633 ⟶ 1,776:
 
=={{header|Haskell}}==
<!--<langsyntaxhighlight lang="haskell">((show :: Integer -> String) . succ . read) "1234"</langsyntaxhighlight>
 
At least one type signature somewhere in the code is necessary, because otherwise there is no specification of what kind of value should be incremented; the operation is equally applicable to any instance of <tt>Enum</tt>:
 
<langsyntaxhighlight lang="haskell">((show :: Bool -> String) . succ . read) "False"</langsyntaxhighlight>-->
 
<langsyntaxhighlight lang="haskell">(show . (+1) . read) "1234"</langsyntaxhighlight>
 
or, for Integral values, we can use the Prelude's '''succ''' function:
 
<langsyntaxhighlight lang="haskell">(show . succ) (read "1234" :: Int)</langsyntaxhighlight>
 
and to extend the range of a succString function to allow for both floating point and integral numeric strings, for non-numeric noise, for multiple numeric expressions within a single string, and for an option to retain or prune any non-numeric noise, we could write things like:
 
{{Trans|Python}}
<langsyntaxhighlight lang="haskell">import Text.Read (readMaybe)
import Data.Maybe (mapMaybe)
 
Line 1,677 ⟶ 1,820:
(putStrLn . unlines) $
succString <$> [True, False] <*>
pure "41.0 pine martens in 1491 -1.5 mushrooms ≠ 136"</langsyntaxhighlight>
{{Out}}
<pre>42.0 1492 -0.5 137
Line 1,683 ⟶ 1,826:
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">CHARACTER string = "123 -4567.89"
 
READ( Text=string) a, b
WRITE(Text=string) a+1, b+1 ! 124 -4566.89</langsyntaxhighlight>
 
=={{header|HolyC}}==
<langsyntaxhighlight lang="holyc">I8 *s;
 
s = "10";
Line 1,697 ⟶ 1,840:
s = "-10";
s = Str2I64(s) + 1;
Print("%d\n", s);</langsyntaxhighlight>
 
=={{header|Hy}}==
<langsyntaxhighlight lang="clojure">(str (inc (int "123")))</langsyntaxhighlight>
Alternatively, with the "threading" macro:
<langsyntaxhighlight lang="clojure">(-> "123" (int) (inc) (str))</langsyntaxhighlight>
 
=={{header|HyperTalk}}==
<langsyntaxhighlight lang="hypertalk">put 0 into someVar
add 1 to someVar
-- without "into [field reference]" the value will appear
-- in the message box
put someVar -- into cd fld 1</langsyntaxhighlight>
 
=={{header|i}}==
<langsyntaxhighlight lang="i">software {
string = "1"
string += 1
print(string)
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon will automatically coerce type conversions where they make sense. Where a conversion can't be made to a required type a run time error is produced.
 
<langsyntaxhighlight Iconlang="icon">s := "123" # s is a string
s +:= 1 # s is now an integer</langsyntaxhighlight>
 
=={{header|IDL}}==
<langsyntaxhighlight lang="idl">str = '1234'
print, string(fix(str)+1)
;==> 1235</langsyntaxhighlight>
 
In fact, IDL tries to convert types cleverly. That works, too:
 
<langsyntaxhighlight lang="idl">print, '1234' + 1
;==> 1235</langsyntaxhighlight>
 
=={{header|Inform 7}}==
This solution works for numbers that fit into a single word (16-bit signed int for [[Z-machine]], 32-bit signed int for [[Glulx virtual machine]]).
<langsyntaxhighlight lang="inform7">Home is a room.
 
To decide which indexed text is incremented (T - indexed text):
Line 1,750 ⟶ 1,893:
When play begins:
say incremented "12345";
end the story.</langsyntaxhighlight>
 
=={{header|Io}}==
<syntaxhighlight lang="text">str := ("123" asNumber + 1) asString</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">incrTextNum=: >:&.".</langsyntaxhighlight>
 
Note that in addition to working for a single numeric value, this will increment multiple values provided within the same string, on a variety of number types and formats including rational and complex numbers (though mixing these notations will coerce all values in the list to a lowest common denominator type).
<langsyntaxhighlight lang="j"> incrTextNum '34.5'
35.5
incrTextNum '7 0.2 3r5 2j4 5.7e_4'
8 1.2 1.6 3j4 1.00057</langsyntaxhighlight>
 
Note also that the result here is a list of characters, and not a list of integers, which becomes obvious when you manipulate the result. For example, consider the effect of reversing the contents of the list:
 
<langsyntaxhighlight lang="j"> |.incrTextNum'123 456'
754 421
|.1+123 456
457 124</langsyntaxhighlight>
 
=={{header|Java}}==
When using <tt>Integer.parseInt</tt> in other places, it may be beneficial to call <tt>trim</tt> on the String, since <tt>parseInt</tt> will throw an Exception if there are spaces in the String.
<langsyntaxhighlight lang="java">String s = "12345";
s = String.valueOf(Integer.parseInt(s) + 1);</langsyntaxhighlight>
 
Another solution that works with big decimal numbers:
<langsyntaxhighlight lang="java">String s = "123456789012345678901234567890.12345";
s = new BigDecimal(s).add(BigDecimal.ONE).toString();</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,785 ⟶ 1,928:
Using implicit coercion:
 
<langsyntaxhighlight lang="javascript">let s = '9999';
let splusplus = (+s+1)+""
 
console.log([splusplus, typeof splusplus]) // 10000,string</langsyntaxhighlight>
 
Or, expanding the range of a '''stringSucc''' function to allow for non-numeric noise, and also for multiple numeric expressions in a single string:
Line 1,794 ⟶ 1,937:
{{Trans|Python}}
{{Trans|Haskell}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,852 ⟶ 1,995:
// MAIN ---
return main();
})();</langsyntaxhighlight>
 
{{Out}}
<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.
 
"1234567889" incstr.</syntaxhighlight>
{{out}}
<pre>"1234567890"</pre>
 
=={{header|jq}}==
====tonumber====
jq's string-to-number filter is called <tt>tonumber</tt>. For example, if we have a file named input.txt consisting of string representations of numbers, one per line, we could compute the sum as follows:
<langsyntaxhighlight lang="sh">$ jq -n -M -s 'map(tonumber) | add' input.txt</langsyntaxhighlight>
 
More precisely, <tt>tonumber</tt> will convert string representations of JSON numbers (integers and decimals) to numbers, but veryjq largeversion integers1.6 and earlier will beconvert convertedvery large integers to decimals with possible loss of precision, and; similar problems will be noticeable for very small and very large decimals. Since the release of jq version 1.6, however, the precision of literal numbers is preserved.
 
The Go implementation of jq, gojq, supports unbounded-precision integer arithmetic, so for example:
<pre>
$ gojq -n '"9" * 100 | tonumber + 1'
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
</pre>
<tt>tostring</tt> can be used to convert numbers to strings.
 
====long_add====
<langsyntaxhighlight lang="jq"># This function assumes its string arguments represent non-negative decimal integers.
def long_add(num1; num2):
if (num1|length) < (num2|length) then long_add(num2; num1)
Line 1,884 ⟶ 2,040:
end )
| reverse | map(.+48) | implode
end ;</langsyntaxhighlight>
'''Example'''
<syntaxhighlight lang="jq">
<lang jq>
long_add("9" * 100; "1")</langsyntaxhighlight>
{{Out}}
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">var a = "1"
a = String(Number(a) + 1)</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">import Base.+
Base.:+(s::AbstractString, n::Real) = string((x = tryparse(Int, s)) isa Int ? x + 1 : parse(Float64, s) + 1)
@show "125" + 1
@show "125.15" + 1
@show "1234567890987654321" + 1
</langsyntaxhighlight>{{out}}
<pre>"125" + 1 = "126"
"125.15" + 1 = "126.15"
Line 1,910 ⟶ 2,066:
"." is a built-in function that evaluates a valid K expression.
 
<langsyntaxhighlight Klang="k"> 1 + ."1234"
1235
1 + ."1234.56"
Line 1,918 ⟶ 2,074:
inc:{1 + . x}
inc "1234"
1235</langsyntaxhighlight>
 
Some other examples.
<langsyntaxhighlight Klang="k"> 1 + .:' ("1";"2";"3";"4")
2 3 4 5
 
Line 1,928 ⟶ 2,084:
 
. "1","+","-10"
-9</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
/** overload ++ operator to increment a numeric string */
Line 1,948 ⟶ 2,104:
ns = "ghijk" // not numeric, so won't be changed by increment operator
println(++ns)
}</langsyntaxhighlight>
 
{{out}}
Line 1,956 ⟶ 2,112:
</pre>
 
=={{header|LambdatalkLabVIEW}}==
{{VI snippet}}<br/>
[[File:LabVIEW_Increment_a_numerical_string.png]]
 
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">
In lambdatalk every expression is a word or an S-expression, so:
 
Line 1,969 ⟶ 2,128:
{+ 123 1} // means "add the words 123 and 1"
-> 124 // can do the job and returns the result as a word
</syntaxhighlight>
</lang>
 
=={{header|LabVIEWLang}}==
<syntaxhighlight lang="lang">
{{VI snippet}}<br/>
$next $= text(+|int({{{123}}}))
[[File:LabVIEW_Increment_a_numerical_string.png]]
</syntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">(integer('123') + 1) -> asstring</langsyntaxhighlight>
-> 124
 
=={{header|LaTeX}}==
<langsyntaxhighlight LaTeXlang="latex">\documentclass{minimal}
\newcounter{tmpnum}
\newcommand{\stringinc}[1]{%
Line 1,989 ⟶ 2,149:
\begin{document}
The number 12345 is followed by \stringinc{12345}.
\end{document}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">' [RC] Increment a numerical string.
 
o$ ="12345"
Line 2,001 ⟶ 2,161:
print o$
 
end</langsyntaxhighlight>
 
=={{header|LIL}}==
<langsyntaxhighlight lang="tcl">##
Increment a numerical string, in LIL
##
set a "41"
inc a
print $a</langsyntaxhighlight>
 
{{out}}
Line 2,016 ⟶ 2,176:
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">put integer("123")+1
-- 124</langsyntaxhighlight>
 
=={{header|LiveCode}}==
LiveCode casts types transparently. When storing a number in a variable, the internal representation is numeric (a double, I think), and if the variable is used as a number, there is no type conversion.
If the variable is used as a string, the conversion is automatic; likewise if a string variable containing a number is used as a number:
<langsyntaxhighlight LiveCodelang="livecode">put "0" & "1234" into myString -- I think this will result in an internal string representation
add 1 to myString -- automatically converts to a number
put "The number is:" && myString
-- outputs "The number is: 1235"</langsyntaxhighlight>
 
=={{header|Logo}}==
Logo is weakly typed, so numeric strings can be treated as numbers and numbers can be treated as strings.
<langsyntaxhighlight lang="logo">show "123 + 1 ; 124
show word? ("123 + 1) ; true</langsyntaxhighlight>
 
=={{header|Logtalk}}==
<langsyntaxhighlight lang="logtalk">number_chars(Number, "123"), Number2 is Number+1, number_chars(Number2, String2)</langsyntaxhighlight>
 
=={{header|LOLCODE}}==
 
LOLCODE is weakly typed, so the arithmetic operators work "as expected" on strings.
 
<langsyntaxhighlight LOLCODElang="lolcode">HAI 1.3
 
I HAS A foo ITZ "1234"
Line 2,045 ⟶ 2,204:
VISIBLE foo BTW, prints 1235
 
KTHXBYE</langsyntaxhighlight>
 
=={{header|LSL}}==
To test it yourself; rez a box on the ground, and add the following as a New Script.
<langsyntaxhighlight LSLlang="lsl">default {
state_entry() {
llListen(PUBLIC_CHANNEL, "", llGetOwner(), "");
Line 2,057 ⟶ 2,216:
llOwnerSay("You said '"+sMessage+"' + 1 = "+(string)(((integer)sMessage)+1));
}
}</langsyntaxhighlight>
{{Out}}
<pre>Increment_a_Numerical_String: You said '99999999' + 1 = 100000000
Line 2,101 ⟶ 2,260:
Val can return type using a special form, using a numeric expression as first parameter: Local m=Val(112+1.12->Decimal) make a new variable m type of decimal (96-bit integer with a variable power of 10).
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
s$ = "12345"
Line 2,112 ⟶ 2,271:
}
CheckIt
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
M4 can handle only integer signed 32 bit numbers, and they can be only written as strings
<langsyntaxhighlight lang="m4">define(`V',`123')dnl
define(`VN',`-123')dnl
eval(V+1)
eval(VN+1)</langsyntaxhighlight>
 
If the expansion of any macro in the argument of eval gives something that can't be interpreted as an expression, an error is raised (but the ''interpretation'' of the whole file is not stopped)
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">s := "12345";
s := convert(parse(s)+1, string);</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">FromDigits["1234"] + 1</langsyntaxhighlight>
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function numStr = incrementNumStr(numStr)
numStr = num2str(str2double(numStr) + 1);
end</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="maxscript">str = "12345"
str = ((str as integer) + 1) as string</langsyntaxhighlight>
 
=={{header|Metafont}}==
<langsyntaxhighlight lang="metafont">string s;
s := "1234";
s := decimal(scantokens(s)+1);
message s;</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<syntaxhighlight lang ="min">(int succ string) :next</langsyntaxhighlight>
 
=={{header|MIPS Assembly}}==
{{trans|Z80 Assembly}}
<syntaxhighlight lang="mips">.include "\SrcAll\Header.asm" ;defines the UserRam label (address 0xA0001000 on N64)
.include "\SrcAll\BasicMacros.asm"
.include "\SrcALL\AdvancedMacros.asm"
.include "\SrcALL\MemoryMap.asm"
 
CursorX equ 0x100 ;offset from label UserRam, used for tracking where to print to the tv screen
CursorY equ 0x101 ;offset from label UserRam, used for tracking where to print to the tv screen
main:
;copy the string to ram.
la $t0,0x00393939 ;store 0,0x39,0x39,0x39 ;n64 is big-endian
la $t1,0x31000000
la $t2,UserRam
sw $t0,0($t2)
nop
sw $t1,4($t2)
nop
;string ram now looks like this:
; byte 0
; byte "9991"
; byte 0
; it was more convenient to store the numeric string little-endian,
; the leading terminator lets us easily print it in reverse.
 
la $t1,UserRam+1 ;read past the terminator placed at the front.
li $t2,0x3A
incStr:
lbu $t0,($t1)
beqz $t0,display
nop
addiu $t0,1
bltu $t0,$t2,noCarry
nop
;carry it forward
li $t0,0x30
sb $t0,($t1)
addiu $t1,1
j incStr
nop
noCarry:
sb $t0,($t1)
addiu $t1,1
j incStr
display:
la $t1,UserRam+4
display_loop:
lbu $t0,($t1)
beqz $t0,shutdown
nop
move $a1,$t0
jal PrintChar ;takes $a1 as argument, prints the ascii character in $a1 to the television screen
nop
subiu $t1,1
j display_loop
nop
shutdown:
nop ;not required on real hardware, but project 64 throws a fit if I don't have this.
b shutdown
nop</syntaxhighlight>
 
{{out}}
<pre>2000</pre>
 
=={{header|mIRC Scripting Language}}==
<langsyntaxhighlight lang="mirc">var %n = 12345
inc %n
echo -ag %n</langsyntaxhighlight>
 
=={{header|ML}}==
==={{header|mLite}}===
<langsyntaxhighlight lang="ocaml">ntos ` ston "1234" + 1;
</syntaxhighlight>
</lang>
 
==={{header|Standard ML}}===
<langsyntaxhighlight lang="sml">Int.toString (1 + valOf (Int.fromString "1234"))</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE addstr;
 
IMPORT InOut, NumConv, Strings;
Line 2,179 ⟶ 2,419:
InOut.WriteString (str2);
InOut.WriteLn
END addstr.</langsyntaxhighlight>
<pre>"12345" + 1 = 12346</pre>
 
=={{header|Modula-3}}==
Modula-3 provides the module <tt>Scan</tt> for lexing.
<langsyntaxhighlight lang="modula3">MODULE StringInt EXPORTS Main;
 
IMPORT IO, Fmt, Scan;
Line 2,194 ⟶ 2,434:
num := Scan.Int(string);
IO.Put(string & " + 1 = " & Fmt.Int(num + 1) & "\n");
END StringInt.</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,202 ⟶ 2,442:
=={{header|MUMPS}}==
Just add. MUMPS has strings of characters as its native datatype. The "+" (plus) binary operator interprets its two arguments as numbers, so the MUMPS system does incrementing a string naturally. MUMPS portability standards require that the result must have at least 15 significant digits. Some implementations use Binary Coded Digits (BCD) and long fixed point (64 bit) integers to accomplish this.
<syntaxhighlight lang="mumps">
<lang MUMPS>
SET STR="123"
WRITE STR+1
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">num = "123"
num = str(int(num) + 1)</langsyntaxhighlight>
 
=={{header|Neko}}==
<langsyntaxhighlight Nekolang="neko">var str = "123";
var str = $string($int(str) + 1);
 
$print(str);</langsyntaxhighlight>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">mutable str = "12345";
str = $"$(Int32.Parse(str)+1)";</langsyntaxhighlight>
 
=={{header|NetRexx}}==
In concert with [[REXX|Rexx]], NetRexx can use typeless variables. Typeless variable support is provided through the default NetRexx <code>'''Rexx'''</code> object. Values are stored as variable length character strings and can be treated as either a string or a numeric value, depending on the context in which they are used.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols nobinary
Line 2,233 ⟶ 2,473:
 
return
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,241 ⟶ 2,481:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(string (++ (int "123")))</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strutils
let next = $(parseInt("123") + 1)</langsyntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 S$ = "12345"
20 S$ = STR$(VAL(s$) + 1)</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
<langsyntaxhighlight lang="oberon2">MODULE addstr;
 
IMPORT Out, Strings;
Line 2,287 ⟶ 2,527:
Out.String (str2);
Out.Ln
END addstr.</langsyntaxhighlight>
Producing:
<pre>jan@Beryllium:~/Oberon/obc$ Add
Line 2,294 ⟶ 2,534:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
s := "12345";
i := int->ToInt(s) + 1;
s := i->ToString();
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">NSString *s = @"12345";
int i = [s intValue] + 1;
s = [NSString stringWithFormat:@"%i", i]</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">string_of_int (succ (int_of_string "1234"))</langsyntaxhighlight>
 
=={{header|Octave}}==
 
We convert the string to a number, increment it, and convert it back to a string.
 
<langsyntaxhighlight lang="octave">nstring = "123";
nstring = sprintf("%d", str2num(nstring) + 1);
disp(nstring);</langsyntaxhighlight>
 
=={{header|Oforth}}==
+ on strings is a concatenation, not an addition. To increment, the string is converted as integer then as string again.
 
<langsyntaxhighlight Oforthlang="oforth">"999" 1 + println
"999" asInteger 1 + asString println</langsyntaxhighlight>
 
{{out}}
Line 2,330 ⟶ 2,569:
=={{header|OoRexx}}==
ooRexx supports the += etc. operators:
<langsyntaxhighlight lang="oorexx">i=1
i+=1
Say i</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,339 ⟶ 2,578:
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight lang="progress">DEFINE VARIABLE cc AS CHARACTER INITIAL "12345".
 
MESSAGE
INTEGER( cc ) + 1
VIEW-AS ALERT-BOX.</langsyntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">{Int.toString {String.toInt "12345"} + 1}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">foo(s)=Str(eval(s)+1);</langsyntaxhighlight>
 
=={{header|Pascal}}==
''See also [[#Free Pascal|Free Pascal]] and [[#Delphi|Delphi]]''
{{works with|Extended Pascal}}
<langsyntaxhighlight lang="pascal">
program TestIncNumString;
type
Line 2,374 ⟶ 2,613:
IncMyString(MyNumString);
write(MyNumString);
END.</langsyntaxhighlight>
{{out}}
<pre>12345 turns into 12346</pre>
Line 2,381 ⟶ 2,620:
Here with different bases upto 10.After this there must be a correction to convert values > '9' to 'A'...
Only for positive integer strings as high speed counter.
<langsyntaxhighlight lang="pascal">program StrInc;
//increments a positive numerical string in different bases.
//the string must be preset with a value, length >0 ;
Line 2,396 ⟶ 2,635:
{$ENDIF}
type
myString = AnsiString; // stringString[32];//
function IncLoop(ps: pChar;le,Base: NativeInt):NativeInt;inline;
//Add 1 and correct carry
//returns 0, if no overflow, else -1
var
dg: nativeInt;
Begin
dec(le);//ps is 0-based
dg := ord(ps[le])+(-ord('0')+1);
result := 0;
 
repeat
IF dg < base then
dg := ord(ps[le])+(-ord('0')+1);
begin
result := -ord(dg>=base);// -1 or 0 -> $FF...FF or $00...00
ps[le] := chr(-(result AND base)+dg+ord('0'));
EXIT;
end;
ps[le] := '0';
dec(le);
until (result dg := 0) or ord(ps[le<])+(-ord('0')+1);
until (le<0);
result:= 1;
end;
 
procedure IncIntStr(base:NativeInt;var s:myString);
var
le: NativeInt;
begin
le := length(s);
//overflow -> prepend a '1' to string
if (IncLoop(pChar(@s[1]),length(s)le,base) <>0) then
Begin
s := '1'+s;
// s := '1'+s;
setlength(s,le+1);
move(s[1],s[2],le);
s[1] := '1';
end;
end;
 
const
ONE_BILLION = 1000*1000*1000;
Line 2,445 ⟶ 2,700:
writeln(s:strLen,' base ',base:2,T0:8:3,' s');
end;
writeln;
writeln('One billion digits "9"');
Line 2,462 ⟶ 2,717:
{$ENDIF}
end.
</syntaxhighlight>
</lang>
{{out|@TIO.RUN}}
<pre>
//sys time for allocating 1 Gb
Real time: 5.512 s User time: 4.068 s Sys. time: 1.288 s CPU share: 97.17 %
 
67108863 increments in base
11111111111111111111111111 base 2 0.451395 s
11200021111001110 base 3 0.394351 s
3333333333333 base 4 0.339301 s
114134440423 base 5 0.363348 s
10354213103 base 6 0.358290 s
1443262443 base 7 0.320281 s
377777777 base 8 0.310279 s
150244043 base 9 0.308271 s
67108863 base 10 0.301267 s // without inline 0.364
 
One billion digits "9"
first 5 digits 09999
1000000001 1.813382 s
first 5 digits 10000
</pre>
 
=={{header|Pebble}}==
<syntaxhighlight lang="pebble">program examples\incstr
 
data
 
int value[0]
str$ text['12345']
 
begin
 
strint [value],text$
+1 [value]
intstr text$,[value]
 
echo text$
pause
kill
 
end</syntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my $s = "12345";
$s++;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"2047"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
2048
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="phixmonti">"12345" tonum 1 + tostr</syntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">$s = "12345";
$s++;</langsyntaxhighlight>
 
=={{header|Picat}}==
===parse_term/1===
<code>parse_term/1</code> is the best to use if there can be no assumption of the type of the number (integer or float). For integers <code>to_int/1</code> is better to use, and for floats <code>to_float/1</code>.
<langsyntaxhighlight Picatlang="picat">go =>
 
% integer
Line 2,517 ⟶ 2,799:
println(to_float=Float.to_float+1),
nl.
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,530 ⟶ 2,812:
===parse_term/3===
<code>parse term/3</code> can evaluate numeric expressions with the help of <code>apply/1</code>:
<langsyntaxhighlight Picatlang="picat">go2 =>
T = "2**16+1",
println(t=T),
parse_term(T,Term, _Vars),
println(term=Term),
println(apply=Term.apply).</langsyntaxhighlight>
 
{{out}}
Line 2,543 ⟶ 2,825:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(format (inc (format "123456")))</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">string number = "0";
number = (string)((int)number+1);
Result: "1"</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">declare s picture '999999999';
s = '123456789';
s = s + 1;
put skip list (s);</langsyntaxhighlight>
<pre>
Note:
Line 2,564 ⟶ 2,846:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To increment a numerical string:
Convert the numerical string to a number.
Add 1 to the number.
Put the number into the numerical string.</langsyntaxhighlight>
 
=={{header|plainTeX}}==
<syntaxhighlight lang="tex">\newcount\acounter
 
<lang tex>\newcount\acounter
\def\stringinc#1{\acounter=#1\relax%
\advance\acounter by 1\relax%
\number\acounter}
The number 12345 is followed by \stringinc{12345}.
\bye</langsyntaxhighlight>
 
The generated page will contain the text:
Line 2,585 ⟶ 2,866:
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">lvars s = '123456789012123456789999999999';
(strnumber(s) + 1) >< '' -> s;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
The easiest way is to cast the string to int, incrementing it and casting back to string:
<langsyntaxhighlight lang="powershell">$s = "12345"
$t = [string] ([int] $s + 1)</langsyntaxhighlight>
One can also take advantage of the fact that PowerShell casts automatically according to the left-most operand to save one cast:
<langsyntaxhighlight lang="powershell">$t = [string] (1 + $s)</langsyntaxhighlight>
 
=={{header|Prolog}}==
Works with SWI-Prolog.
<langsyntaxhighlight Prologlang="prolog">incr_numerical_string(S1, S2) :-
string_to_atom(S1, A1),
atom_number(A1, N1),
Line 2,603 ⟶ 2,884:
atom_number(A2, N2),
string_to_atom(S2, A2).
</syntaxhighlight>
</lang>
{{Out}}
<langsyntaxhighlight Prologlang="prolog"> ?- incr_numerical_string("123", S2).
S2 = "124".
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">string$="12345"
string$=Str(Val(string$)+1)
Debug string$</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|2.3 through 3.4}}
<langsyntaxhighlight lang="python">next = str(int('123') + 1)</langsyntaxhighlight>
 
Or, preserving the distinction between integer and floating point numeric values, while also allowing for noisy or multi-number numerical strings, and providing the option of retaining or pruning out any non-numeric parts of the string.
<langsyntaxhighlight lang="python"># Dropping or keeping any non-numerics in the string
 
 
Line 2,654 ⟶ 2,935:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>42.0 pine martens in 1492 -0.5 mushrooms ≠ 137
Line 2,660 ⟶ 2,941:
 
=={{header|Quackery}}==
 
As a dialogue in the Quackery shell.
 
Line 2,685 ⟶ 2,965:
 
=={{header|R}}==
<langsyntaxhighlight lang="r">s = "12345"
s <- as.character(as.numeric(s) + 1)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define next (compose number->string add1 string->number))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<syntaxhighlight lang="raku" perl6line>say my $s = "12345";
say ++$s;
$s++;</lang>
 
# or Unicode. How about Sinhala?
 
say "෧෨෩෪෫ ", +"෧෨෩෪෫";
say "෧෨෩෪෫".succ, ' ', +"෧෨෩෪෫".succ;</syntaxhighlight>
{{out}}
<pre>12345
12346
෧෨෩෪෫ 12345
෧෨෩෪෬ 12346</pre>
 
=={{header|Rascal}}==
<langsyntaxhighlight lang="rascal">
import String;
 
public str IncrNumStr(str s) = "<toInt(s) + 1>";
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,714 ⟶ 3,003:
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">REBOL [
Title: "Increment Numerical String"
URL: http://rosettacode.org/wiki/Increment_numerical_string
Line 2,734 ⟶ 3,023:
print [x: "-99" "plus one equals" mold s++ x]
print [x: "42" "plus one equals" mold s++ x]
print [x: "12345" "plus one equals" mold s++ x]</langsyntaxhighlight>
 
{{Out}}
Line 2,742 ⟶ 3,031:
 
=={{header|Retro}}==
<syntaxhighlight lang="retro">'123 s:to-number n:inc n:to-string</syntaxhighlight>
 
<lang Retro>'123 s:to-number n:inc n:to-string</lang>
 
=={{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
<br>either a string or a numeric value, depending on the context in which they are used.
===version 1===
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates a method how to increment a numerical string*/
count = "3" /*REXX variables (and constants) are character strings.*/
count = 3 /*(identical to the above statement.) */
Line 2,771 ⟶ 3,058:
/* [↓] show the six leftmost and rightmost digs.*/
say 'count=' left(count,6)'···'right(count,6)
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,781 ⟶ 3,068:
Looking at the PL/I code I started investigating this situation in Rexx.
These are my findings:
<langsyntaxhighlight lang="rexx">/* REXX ************************************************************
* There is no equivalent to PL/I's SIZE condition in REXX.
* The result of an arithmetic expression is rounded
Line 2,815 ⟶ 3,102:
Say 'LOSTDIGITS condition raised in line' sigl
Say 'sourceline='sourceline(sigl)
Say "condition('D')="condition('D')</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,832 ⟶ 3,119:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
x = "1234" See 1+x # print 1235
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
Conversion to/from a real number is the most convenient way to perform the task.
{{in}}
<pre>
"1234" STR→ 1 + →STR
"99.9" STR→ 1 + →STR
</pre>
{{out}}
<pre>
2: "1235"
1: "100.9"
</pre>
 
=={{header|Ruby}}==
If a string represents a number, the succ method will increment the number:
<langsyntaxhighlight lang="ruby">'1234'.succ #=> '1235'
'99'.succ #=> '100'</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
Run BASIC has trim command for left and right
<langsyntaxhighlight lang="runbasic">string$ = "12345"
numeric = val(string$)
numeric = numeric + 1
string$ = str$(numeric)
print string$
</syntaxhighlight>
</lang>
<pre>12346</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn next_string(input: &str) -> String {
(input.parse::<i64>().unwrap() + 1).to_string()
}
Line 2,860 ⟶ 3,160:
let s2 = next_string(s);
println!("{:?}", s2);
}</langsyntaxhighlight>
{{out}}
<pre>"0"</pre>
Line 2,868 ⟶ 3,168:
handle most numeric strings. We define a method to do it.
 
<langsyntaxhighlight lang="scala">implicit def toSucc(s: String) = new { def succ = BigDecimal(s) + 1 toString }</langsyntaxhighlight>
 
Usage:
Line 2,878 ⟶ 3,178:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(number->string (+ 1 (string->number "1234")))</langsyntaxhighlight>
 
=={{header|Sed}}==
 
=={{header|sed}}==
Reads a decimal integer from stdin and outputs the same with the magnitude incremented by one.
 
(TODO: Since it deals only with the magnitude, the result is incorrect for negative numbers—though adding this support is definitely possible.)
 
The following happens:
The routine starts by suffixing the input number with a carry mark (a <code>:</code> in this case) indicating that the digit to its left still needs to be incremented. In a loop, the following happens:
* prepend zero, if only nines (there will be an overflow) or empty
 
* remember the number (in hold space)
* If there is a carry mark on the far left, replace it with a 1.
* increment all digits
* If there are no more carry marks, exit the loop.
* Holdappend the currentnew number. (<code>h</code>)to the old one
* Extractcut theout digiteverything tobetween the lefttwo positions of the firsthighest carry mark. (<code>s</code>)
* Replace the digit with the same digit incremented by one, with 9 incrementing to a carry mark (i.e. 10). (<code>y</code>)
* If the result of such replacement was a carry mark, suffix the mark with a 0, indicating that the digit has rolled over and the digit to the left must be incremented. (<code>s</code>)
* Retrieve the held number (<code>G</code>) and replace the first carry mark and the digit to its left with the result of the computation. (<code>s</code>)
* Repeat. (<code>b</code>)
 
<langsyntaxhighlight lang="sed">s/^.9*$/0&:/
h
:bubble
y/0123456789/1234567890/
s/^:/1/
x
/.:/ {
G
h
s/^.9*\(n.*\([^0]\):.*$/\1/</syntaxhighlight>
y/0123456789/123456789:/
s/:/:0/
G
s/\(.*\)\n\(.*\).:\(.*\)$/\2\1\3/
b bubble
}</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">var string: s is "12345";
 
s := str(succ(integer parse s));</langsyntaxhighlight>
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">
put "123" + 1 // 124
</syntaxhighlight>
</lang>
 
=={{header|SequenceL}}==
<langsyntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
 
increment(input(1)) := intToString(stringToInt(input) + 1);</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say '1234'.inc; #=> '1235'
say '99'.inc; #=> '100'</langsyntaxhighlight>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">((Integer readFrom: '123') + 1) printString</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
 
<langsyntaxhighlight lang="smalltalk">('123' asInteger + 1) printString</langsyntaxhighlight>
(a note to non-smalltalkers: "printString" does not print, but return the "printString")
 
=={{header|SNOBOL4}}==
<langsyntaxhighlight lang="snobol4">
output = trim(input) + 1
output = "123" + 1
end</langsyntaxhighlight>
 
<pre>
Line 2,951 ⟶ 3,240:
124
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "incstr" )
@( description, "Increment an integer number in a string" )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Increment_a_numerical_string" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure incstr is
s : string := "12345";
begin
s := strings.trim( strings.image( integer( numerics.value( s ) + 1 ) ), trim_end.both ) ;
? s;
end incstr;</syntaxhighlight>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">function numStrIncmt(s) {
return fmtstr("%d", toint(s) + 1);
}
 
spn:1> numStrIncmt("12345")
= 12346</langsyntaxhighlight>
 
=={{header|SuperTalk}}==
<langsyntaxhighlight lang="supertalk">put 0 into someVar
add 1 to someVar
-- without "into [field reference]" the value will appear
-- in the message box
put someVar -- into cd fld 1</langsyntaxhighlight>
 
=={{header|Swift}}==
{{works with|Swift|2.x+}}
<langsyntaxhighlight lang="swift">let s = "1234"
if let x = Int(s) {
print("\(x + 1)")
}</langsyntaxhighlight>
{{works with|Swift|1.x}}
<langsyntaxhighlight lang="swift">let s = "1234"
if let x = s.toInt() {
println("\(x + 1)")
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
In the end, all variables are strings in Tcl. A "number" is merely a particular interpretation of a string of bytes.
<langsyntaxhighlight lang="tcl">set str 1234
incr str</langsyntaxhighlight>
 
=={{header|TI-83 BASIC}}==
There is no single command to convert a number to a string; you have to store it to one of the Function variables which acts as both a number and a string.
<langsyntaxhighlight lang="ti83b">:"1"→Str1
:expr(Str1)+1→A
:{0,1}→L₁
Line 2,992 ⟶ 3,301:
:LinReg(ax+b) Y₁
:Equ►String(Y₁,Str1)
:sub(Str1,1,length(Str1)-3)→Str1</langsyntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
<syntaxhighlight lang ="ti89b">string(expr(str)+1)</langsyntaxhighlight>
 
=={{header|Toka}}==
<langsyntaxhighlight lang="toka">" 100" >number drop 1 + >string</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
Line 3,011 ⟶ 3,320:
$string += 10;
$string is now 12355.
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">
(with s "12345"
(= s String((+ (to-Int s) 1))))
(textout s))
</syntaxhighlight>
{{out}}
<pre>
12346
</pre>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
teststring="0'1'-1'12345'10000000'-10000000"
Line 3,020 ⟶ 3,340:
PRINT n
ENDLOOP
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,032 ⟶ 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.)
 
==== TXR Lisp ====
 
<langsyntaxhighlight lang="txr">@(do (defun inc-num-str (str-in)
(let ((len (length str-in))
(str (copy-str str-in)))
Line 3,047 ⟶ 3,366:
(set [str i] #\0))))))
@(bind a @(inc-num-str "9999"))
@(bind b @(inc-num-str "1234"))</langsyntaxhighlight>
 
<pre>$ ./txr -B incnum.txr
Line 3,055 ⟶ 3,374:
==== No TXR Lisp ====
 
<langsyntaxhighlight lang="txr">@(deffilter incdig ("0" "1") ("1" "2") ("2" "3") ("3" "4") ("4" "5")
("5" "6") ("6" "7") ("7" "8") ("8" "9"))
@(define increment (num out))
Line 3,075 ⟶ 3,394:
@(end)
@in
@(increment in out)</langsyntaxhighlight>
 
<pre>$ echo 1 | ./txr -B incnum.txr -
Line 3,097 ⟶ 3,416:
 
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash"># All variables are strings within the shell
# Although num look like a number, it is in fact a numerical string
num=5
num=`expr $num + 1` # Increment the number</langsyntaxhighlight>
 
The [[Korn Shell]] and some newer shells do support arithmetic operations directly, and several syntax options are available:
Line 3,108 ⟶ 3,427:
{{works with|pdksh}}
{{works with|zsh}}
<langsyntaxhighlight lang="bash">num=5
let num=num+1 # Increment the number
let "num = num + 1" # Increment again. (We can use spaces inside quotes)
Line 3,114 ⟶ 3,433:
let num+=1 # This time we use +=
let "num += 1"
((num += 1))</langsyntaxhighlight>
 
{{works with|ksh93}}
{{works with|pdksh}}
{{works with|zsh}}
<langsyntaxhighlight lang="bash">integer num=5 # Declare an integer...
num=$num+1 # ...then increment without the let keyword.</langsyntaxhighlight>
 
==={{header|C Shell}}===
The <code>@</code> assignment command uses strings as integers.
<langsyntaxhighlight lang="csh">@ num = 5
@ num += 1</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl string num
set num "123"
set num (int (+ (int num) 1))</langsyntaxhighlight>
 
=={{header|Ursala}}==
<syntaxhighlight lang="ursala">#import nat
 
instring = ~&h+ %nP+ successor+ %np@iNC # convert, do the math, convert back</syntaxhighlight>
<lang Ursala>#import nat
 
instring = ~&h+ %nP+ successor+ %np@iNC # convert, do the math, convert back</lang>
test program:
<langsyntaxhighlight Ursalalang="ursala">#cast %sL
 
tests = instring* <'22435','4','125','77','325'></langsyntaxhighlight>
{{Out}}
<pre>
Line 3,148 ⟶ 3,466:
=={{header|VBA}}==
The easy method assumes that the number can be represented as a Long integer:
<syntaxhighlight lang="vba">
<lang VBA>
Public Function incr(astring As String) As String
'simple function to increment a number string
incr = CStr(CLng(astring) + 1)
End Function
</syntaxhighlight>
</lang>
Examples:
<pre>
Line 3,163 ⟶ 3,481:
 
The long version handles arbitrary-length strings:
<syntaxhighlight lang="vba">
<lang VBA>
Public Function Lincr(astring As String) As String
'increment a number string, of whatever length
Line 3,228 ⟶ 3,546:
decrement = result
End Function
</syntaxhighlight>
</lang>
Examples:
<pre>
Line 3,247 ⟶ 3,565:
This example increments numeric string by converting it into numeric value, as most other language examples do.
The string is located in text register 10.
<langsyntaxhighlight lang="vedit">itoa(atoi(10)+1, 10)</langsyntaxhighlight>
 
The following example increments unsigned numeric string of unlimited length.
The current line in the edit buffer contains the string.
<langsyntaxhighlight lang="vedit">EOL
do {
if (At_BOL) {
Line 3,266 ⟶ 3,584:
Ins_Char(#1, OVERWRITE)
Char(-1)
} while (#2) // repeat until no carry</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
<langsyntaxhighlight lang="vbnet"> Dim s As String = "123"
 
s = CStr(CInt("123") + 1)
' or
s = (CInt("123") + 1).ToString</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
<langsyntaxhighlight lang="go">// Increment a numerical string in V
module main
 
Line 3,292 ⟶ 3,610:
println("numstr: $numstr")
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,304 ⟶ 3,622:
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var ns = "41"
var n = Num.fromString(ns) + 1
var ns2 = "%(n)"
System.print("%(ns) + 1 = %(ns2)")</langsyntaxhighlight>
 
{{out}}
Line 3,315 ⟶ 3,633:
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="lisp">(DEFUN INCREMENT-STRING (X)
(NUMBER->STRING (+ (STRING->NUMBER X) 1)))</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated string convention
code Text=12;
 
Line 3,341 ⟶ 3,659:
IncStr(Str); IncStr(Str);
Text(0, Str);
]</langsyntaxhighlight>
 
{{out}}
Line 3,347 ⟶ 3,665:
0124000000001
</pre>
 
=={{header|Z80 Assembly}}==
As a test case, we'll start with 1999 and increment it to 2000.
<syntaxhighlight lang="z80">;;;;;;;;;;;;;;;;;;; HEADER ;;;;;;;;;;;;;;;;;;;
read "\SrcCPC\winape_macros.asm"
read "\SrcCPC\MemoryMap.asm"
read "\SrcALL\winapeBuildCompat.asm"
;;;;;;;;;;;;;;;;;;; PROGRAM ;;;;;;;;;;;;;;;;;;;
 
org &1000
ld hl,NumericString
 
incstring:
ld a,(hl)
cp 255
jr z,displaystring
add &01
cp &3A
jr nz,displaystring
;carry forward
ld a,&30
ld (hl),a
inc hl
jr incstring
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
displaystring:
ld (hl),a ;store the last addition.
ld hl,NumericString_End
dec hl
disploop:
ld a,(hl)
cp 255
ret z
call &bb5a
dec hl
jr disploop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
db 255 ;terminator when reading in reverse
NumericString:
;stored little-endian for convenience
db &39,&39,&39,&31
NumericString_End:
db 255 ;terminator
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
read "\SrcCPC\winape_showhex.asm"
read "\SrcCPC\winape_stringop.asm"</syntaxhighlight>
 
{{out}}
<pre>2000</pre>
 
=={{header|zkl}}==
In zkl, the first operand "wins" and transforms the second. So 5+"1"-->6
<langsyntaxhighlight lang="zkl">fcn numStringPlusOne(s){1+s}
numStringPlusOne("123") //-->124</langsyntaxhighlight>
 
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
<lang Zoea>
program: increment_a_numerical_string
case: 1
Line 3,362 ⟶ 3,729:
input: '19'
output: '20'
</syntaxhighlight>
</lang>
 
=={{header|Zoea Visual}}==
305

edits