Increment a numerical string: Difference between revisions

From Rosetta Code
Content added Content deleted
(add E example)
(Add Seed7 example)
Line 219: Line 219:
str="123456"
str="123456"
i=str.to_i+1
i=str.to_i+1

==[[Seed7]]==
[[Category:Seed7]]
var string: s is "12345";

s := str(succ(integer parse s));


==[[Tcl]]==
==[[Tcl]]==

Revision as of 13:25, 27 March 2007

Task
Increment a numerical string
You are encouraged to solve this task according to the task description, using any language you may know.

This task is about incrementing a numerical string.

Ada

The standard Ada package Ada.Strings.Fixed provides a function for trimming blanks from a string.

S : String := "12345";
S := Ada.Strings.Fixed.Trim(Source => Integer'Image(Integer'Value(S) + 1), Side => Ada.Strings.Both);

BASIC

Interpeter: QuickBasic 4.5, PB 7.1

 s$ = "12345"
 i% = VAL(s$) + 1
 s$ = STR$(i%)

C

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 char s[] = "12345";
 // Using atoi
 int i = atoi(s) + 1;
 // Using strtol
 int i = (int)strtol(s, NULL, 10) + 1;
 
 itoa(i, s, 10); 


C++

Libraries: STL

 // STL with string stream operators
 #include <cstdlib>
 #include <string>
 #include <sstream>
 using namespace std;
 std::string s = "12345";
 int i = atoi((const char*)s.c_str()) + 1;
 std::ostringstream oss;
 if (oss << i) s = oss.str();

Libraries: Boost

 // Boost
 #include <cstdlib>
 #include <string>
 #include <boost/lexical_cast.hpp>
 
 std::string s = "12345";
 int i = boost::lexical_cast<int>(s) + 1;
 s = boost::lexical_cast<std::string>(i);

Libraries: Qt

 // Qt
 QString s = "12345";
 int i = atoi((const char*)s.data()) + 1;
 // Using constructor(int, base)
 QString s2(i, 10);
 s = s2;
 // Using setNum(int)
 s.setNum(i);


Libraries: Microsoft Foundation Classes

 // MFC
 CString s = "12345";
 int i = _ttoi(s) + 1;
 int i = _tcstoul(s, NULL, 10) + 1; 
 s.Format("%d", i);

All of the above solutions only work for numbers <= INT_MAX. The following works for an (almost) arbitrary large number:

Compiler: GCC 4.0.2

 #include <string>
 #include <iostream>
 #include <ostream>
 
 void increment_numerical_string(std::string& s)
 {
   std::string::reverse_iterator iter = s.rbegin(), end = s.rend();
   int carry = 1;
   while (carry && iter != end)
   {
     int value = (*iter - '0') + carry;
     carry = (value / 10);
     *iter = '0' + (value % 10);
     ++iter;
   }
   if (carry)
     s.insert(0, "1");
 }
 
 int main()
 {
   std::string big_number = "123456789012345678901234567899";
   std::cout << "before increment: " << big_number << "\n";
   increment_numerical_string(big_number);
   std::cout << "after increment:  " << big_number << "\n";
 }

C#

 string s = "12345";
 int i = int.Parse(s) + 1;
 s = i.ToString();

D

Compiler: DMD,GDC

E

__makeInt("1234", 10).next().toString(10)

Forth

This word causes the number whose string value is stored at the given location to be incremented. The address passed must contain enough space to hold the string representation of the new number. Error handling is rudimentary, and consists of aborting when the string does not contain a numerical value.

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.

: >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 ;

Here is a version that can increment by any value

: inc-string ( addr n -- )
  over count number? not abort" invalid number"
  rot s>d d+  >string rot place ;

Test the first version like this:

s" 123" pad place
pad inc-string
pad count type

And the second one like this:

s" 123" pad place
pad 1 inc-string
pad count type

IDL

str = '1234'
print, string(fix(str)+1)
;==>   1235

Java

 String s = "12345";
 int i = Integer.parseInt(s, 10) + 1;
 s = Integer.toString(i);

JavaScript

 var s = "12345";
 var i = parseInt(s) + 1;
 s = i;

LaTeX

\documentclass{article}

% numbers are stored in counters
\newcounter{tmpnum}

% macro to increment a string (given as argument)
\newcommand{\stringinc}[1]{%
\setcounter{tmpnum}{#1}%     setcounter effectively converts the string to a number
\stepcounter{tmpnum}%        increment the counter; alternatively: \addtocounter{tmpnum}{1}
\arabic{tmpnum}%             convert counter value to arabic (i.e. decimal) number string
}

%example usage
\begin{document}
The number 12345 is followed by \stringinc{12345}.
\end{document}

Perl

Interpeter: Perl

 my $s = "12345";
 $s++;

PHP

 $s = "12345";
 $s++;

Python

Interpeter: Python 2.3, 2.4, 2.5

 s = int("12345")
 s += 1

Ruby

 str="123456"
 i=str.to_i+1

Seed7

 var string: s is "12345";
 s := str(succ(integer parse s));

Tcl

In the end, all variables are strings in Tcl. A "number" is merely a particular interpretation of a string of bytes.

set str 1234
incr str