Task

Increment a numerical string.




MODULE addstr;
 
IMPORT  Out, Strings;
 
VAR     str1, str2      : ARRAY 9 OF CHAR;
        num, pos        : INTEGER;
        carry           : BOOLEAN;
        ch              : CHAR;
 
BEGIN
  str1 := "9999";
  Out.Char ('"');       Out.String (str1);      Out.String ('" + 1 = ');
  num := Strings.Length (str1) - 1;
  pos := num;
  IF  str1 [0] = '9'  THEN  INC (pos)  END;
  str2 [pos + 1] := 0X;
  carry := TRUE;
  REPEAT
    ch := str1 [num];
    IF  carry  THEN
      ch := CHR (ORD (ch) + 1)
    END;
    IF  ch > '9'  THEN
      carry := TRUE;
      ch := '0'
    ELSE
      carry := FALSE
    END;
    str2 [pos] := ch;
    DEC (num);
    DEC (pos)
  UNTIL num < 0;
  IF  carry  THEN  str2 [0] := '1'  END;
  Out.String (str2);
  Out.Ln
END addstr.