Question

I need to create a program where in the first like of input I would put the number of numbers in the string separated by space, and in the second line I would enter them (for example 1 2 3 4 5 6). So I tried using Val, but it can't help me because there are spaces, also I can't use for because the numbers are in one line. Also numbers don't have to be one figure, they are from 1 to 10^9 .

Was it helpful?

Solution

This is the principle: Let s be your string. In a loop to the following

  1. Remove leading spaces in s

    while (length(s)>0) and (s[1]=' ') do delete(s,1,1);

  2. Find the first space in the remaining string

    p := pos(' ',s);

  3. Copy the non-space part leading in a second string t and remove that part from s

    t := copy(s,1,p-1); delete(s,1,p);

  4. get the next array element element[i] with

    val(t,element[i],code);

Of course you have to code checks (Is the string s empty after some manipulation? If val gives an error, you have no valid number,...), array indexing etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top