0

How can I convert a string of numbers like for example '21354561321535613' into digits and store them in an array?

Each digit should be turned into an integer element in the array, so the string '21354561321535613' should result in:

[2, 1, 3, 5, 4, 5, 6, 1, 3, 2, 1, 5, 3, 5, 6, 1, 3]
3
  • 2
    StrToInt and For-in Commented Dec 19, 2016 at 12:42
  • Should each digit be turned into an element in the array? Commented Dec 19, 2016 at 18:51
  • yes,each digit be turned into an element in the array Commented Dec 19, 2016 at 19:14

4 Answers 4

3

You can easily turn a digit into an integer by subtracting the ordinal value of '0'. Do this in a loop and you have an integer for each digit:

var
  S: string;
  A: array of Integer;
  I, Len: Integer;
begin
  S := '21354561321535613';
  Len := Length(S);

  { Reserve Len Integers. }
  SetLength(A, Len);

  { Convert each digit into an integer: }
  for I := 1 to Len do
    A[I - 1] := Ord(S[I]) - Ord('0'); { [I - 1] because array is zero-based. }
end;
Sign up to request clarification or add additional context in comments.

14 Comments

can i ask another question please,why when i use a function from a unit, and this function contains an array, the array will be one-based ?
If you want to ask another question, then, well, ask another question. Anyway, in Pascal, static arrays (e.g. array[7..12] of Integer) can have any base (in this case, 7). The index can be any ordinal, so even Char or Boolean or an enumerated type. Dynamic arrays (e.g. array of Integer if declared as var or type) are always zero-based and the index is always an integer. The A in my answer is such a dynamic array, and is thus always zero-based.
Please do yourself a favour and read the documentation you have for your dialect of Pascal very carefully. For Delphi, that is the Delphi (or Object Pascal) Language Guide and is part of the help that comes with it. For FreePascal, you can find something like it online. For other Pascals, you'll have to find it yourself.
I got it!, it's very helpful. then i will take my chance and ask, is there a type bigger then int64 in pascal ? (because i had to calculate 100! to use it in a problem).
There is no built-in integer type that is larger than UInt64. If you need something bigger, try my BigInteger implementation. It can handle 100! and even 1000! easily.
|
0
var
  Str: string;
  Arr: array of Integer;
  i: Integer;
  Len: Integer;
begin
  Str := '21354561321535613';
  Len := Length(Str);
  SetLength(arr, Len);

  for i := 1 to Len do
    Arr[i - 1] := StrToInt(Str[i]);  
end;

Comments

0

You can use this code to covert String to Array of bytes)

uses crt;
var
        s:string;
        a:array[1..1000] of byte;
        i:byte;
begin
        s:='1234567';
        for i:=1 to length(s) do
                val(s[i],a[i]);
end.

Comments

-2

it sample to convert numbers as string to numbers use the function

 strtoint(number_in_string);

to convert numbers to numbers as string use the function

inttostr(number);

1 Comment

That's not what is being asked for. They want to have an array of digits, not a single integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.