I have written this program ,but i found some problem that I cannot solve.The point is in the function ,I don't know how many parameter.
This may be 2 or 3 or 4.THANK YOU!!!
program test;
var
k : string;
function add(st : array of string) : string;
var
i : integer;
begin
for i := 1 to high(st) do
add := add + st[i];
end;
begin
k := add('abc','sdgsdg');
writeln(k);
end.
Comments
: This may be 2 or 3 or 4.THANK YOU!!!
:
: program test;
: var
: k : string;
:
: function add(st : array of string) : string;
: var
: i : integer;
: begin
: for i := 1 to high(st) do
: add := add + st[i];
: end;
:
: begin
: k := add('abc','sdgsdg');
: writeln(k);
: end.
:
I hope I'm not misunderstanding your problem, but if you are trying to concatenate those two strings, you will have to pass two parameters to your function. You don't need to loop thru the string. Just do:
[code]
function add(st1 :string; st2:string):string;
begin
add:=st1+st2;
end;
[/code]
If you mean that you may want to concatenate 2,3,or 4 strings, you could pass an array of strings and an integer to specify the number of strings in your array that you wish to concatenate.
The following statement should be write
How to write the function??
k assign to string
This program like concat function
But I wanted to write it
k := abc('a','f');
k := abc('fv,'h','sdf');
: : I have written this program ,but i found some problem that I cannot solve.The point is in the function ,I don't know how many parameter.
: : This may be 2 or 3 or 4.THANK YOU!!!
: :
: : program test;
: : var
: : k : string;
: :
: : function add(st : array of string) : string;
: : var
: : i : integer;
: : begin
: : for i := 1 to high(st) do
: : add := add + st[i];
: : end;
: :
: : begin
: : k := add('abc','sdgsdg');
: : writeln(k);
: : end.
: :
: I hope I'm not misunderstanding your problem, but if you are trying to concatenate those two strings, you will have to pass two parameters to your function. You don't need to loop thru the string. Just do:
:
: [code]
: function add(st1 :string; st2:string):string;
: begin
: add:=st1+st2;
: end;
: [/code]
:
: If you mean that you may want to concatenate 2,3,or 4 strings, you could pass an array of strings and an integer to specify the number of strings in your array that you wish to concatenate.
:
: The following statement should be write
: How to write the function??
: k assign to string
: This program like concat function
: But I wanted to write it
: k := abc('a','f');
: k := abc('fv,'h','sdf');
:
:
: : : I have written this program ,but i found some problem that I cannot solve.The point is in the function ,I don't know how many parameter.
: : : This may be 2 or 3 or 4.THANK YOU!!!
: : :
: : : program test;
: : : var
: : : k : string;
: : :
: : : function add(st : array of string) : string;
: : : var
: : : i : integer;
: : : begin
: : : for i := 1 to high(st) do
: : : add := add + st[i];
: : : end;
: : :
: : : begin
: : : k := add('abc','sdgsdg');
: : : writeln(k);
: : : end.
: : :
: : I hope I'm not misunderstanding your problem, but if you are trying to concatenate those two strings, you will have to pass two parameters to your function. You don't need to loop thru the string. Just do:
: :
: : [code]
: : function add(st1 :string; st2:string):string;
: : begin
: : add:=st1+st2;
: : end;
: : [/code]
: :
: : If you mean that you may want to concatenate 2,3,or 4 strings, you could pass an array of strings and an integer to specify the number of strings in your array that you wish to concatenate.
: :
:
:
The only ways you can solve this problem is by using linked lists or a recursive function. The way you want to is impossible in most Pascal versions, because the write and read statements are not compiled as normal statements.
Here is the recursive solution using Perran's function:
[code]
k := Add('a', 'f'); { adding two strings }
k := Add('fv', Add('h', 'sdf')); { adding three strings }
k := Add('fv', Add('h', Add('sdf', 'hjk'))); { adding four strings }
[/code]
your array needs to be zero-based.
Instead of:
for i := 1 to high(st) do
Try this:
for i := 0 to high(st) do
Or maybe:
for i := low(st) to high(st) do
It is usually a good idea to initialize local variables first:
add:='';
AFAIK, Turbo Pascal does not support strings > 255 chars,
so there is a good chance your result may be truncated if
you feed it too much data.
: I have written this program ,but i found some problem that I cannot solve.The point is in the function ,I don't know how many parameter.
: This may be 2 or 3 or 4.THANK YOU!!!
:
: program test;
: var
: k : string;
:
: function add(st : array of string) : string;
: var
: i : integer;
: begin
: for i := 1 to high(st) do
: add := add + st[i];
: end;
:
: begin
: k := add('abc','sdgsdg');
: writeln(k);
: end.
:
but it said
"Free Pascal Compiler version 1.0.6 [2002/04/23] for i386
Copyright (c) 1993-2002 by Florian Klaempfl
Target OS: Win32 for i386
Compiling c:pascal_pgmsourceconcat.pas
concat.pas(15,19) Error: Wrong amount of parameters specified
concat.pas(16,7) Fatal: There were 1 errors compiling module, stopping"
It looks to me like you have the right idea, but I think
: your array needs to be zero-based.
: Instead of:
: for i := 1 to high(st) do
: Try this:
: for i := 0 to high(st) do
: Or maybe:
: for i := low(st) to high(st) do
:
:
:
: It is usually a good idea to initialize local variables first:
: add:='';
:
:
: AFAIK, Turbo Pascal does not support strings > 255 chars,
: so there is a good chance your result may be truncated if
: you feed it too much data.
:
:
:
:
: : I have written this program ,but i found some problem that I cannot solve.The point is in the function ,I don't know how many parameter.
: : This may be 2 or 3 or 4.THANK YOU!!!
: :
: : program test;
: : var
: : k : string;
: :
: : function add(st : array of string) : string;
: : var
: : i : integer;
: : begin
: : for i := 1 to high(st) do
: : add := add + st[i];
: : end;
: :
: : begin
: : k := add('abc','sdgsdg');
: : writeln(k);
: : end.
: :
:
[code]
k := add(['abc','sdgsdg']);
[/code]
The array-brackets ("[" & "]") are important here to make the parameter an array.
: I have try this
: but it said
:
: "Free Pascal Compiler version 1.0.6 [2002/04/23] for i386
: Copyright (c) 1993-2002 by Florian Klaempfl
: Target OS: Win32 for i386
: Compiling c:pascal_pgmsourceconcat.pas
: concat.pas(15,19) Error: Wrong amount of parameters specified
: concat.pas(16,7) Fatal: There were 1 errors compiling module, stopping"
:
: It looks to me like you have the right idea, but I think
: : your array needs to be zero-based.
: : Instead of:
: : for i := 1 to high(st) do
: : Try this:
: : for i := 0 to high(st) do
: : Or maybe:
: : for i := low(st) to high(st) do
: :
: :
: :
: : It is usually a good idea to initialize local variables first:
: : add:='';
: :
: :
: : AFAIK, Turbo Pascal does not support strings > 255 chars,
: : so there is a good chance your result may be truncated if
: : you feed it too much data.
: :
: :
: :
: :
: : : I have written this program ,but i found some problem that I cannot solve.The point is in the function ,I don't know how many parameter.
: : : This may be 2 or 3 or 4.THANK YOU!!!
: : :
: : : program test;
: : : var
: : : k : string;
: : :
: : : function add(st : array of string) : string;
: : : var
: : : i : integer;
: : : begin
: : : for i := 1 to high(st) do
: : : add := add + st[i];
: : : end;
: : :
: : : begin
: : : k := add('abc','sdgsdg');
: : : writeln(k);
: : : end.
: : :
: :
:
:
: parameter an array.
You are right, zibadian - I overlooked that one!
FreePascal does support long strings, but you must compile
with the {$H+} option.
but I wanted to write a function is concat
because I interesting in string function
How to write a concat function that do not need ("[" & "])"
: : The array-brackets ("[" & "])" are important here to make the
: : parameter an array.
:
: You are right, zibadian - I overlooked that one!
:
: FreePascal does support long strings, but you must compile
: with the {$H+} option.
:
: Yes I can write this program
: but I wanted to write a function is concat
: because I interesting in string function
: How to write a concat function that do not need ("[" & "])"
: : : The array-brackets ("[" & "])" are important here to make the
: : : parameter an array.
: :
: : You are right, zibadian - I overlooked that one!
: :
: : FreePascal does support long strings, but you must compile
: : with the {$H+} option.
: :
:
:
: The point is in the function ,I don't know how many parameter.
: This may be 2 or 3 or 4.THANK YOU!!!
If you know that the function always takes 2 or 3 or 4 parameters
( This is what you say )
You can also use overloaded functions:
[code]
function add(s1, s2:string):string; overload;
begin
add := s1 + s2;
end;
function add(s1, s2, s3:string):string; overload;
begin
add := s1 + s2 + s3;
end;
function add(s1, s2, s3, s4:string):string; overload;
begin
add := s1 + s2 + s3 + s4;
end;
[/code]
I wanted to write a program like concat
Concat can support many parameters or 2 parameters
: : I have written this program ,but i found some problem that I cannot solve.
: : The point is in the function ,I don't know how many parameter.
: : This may be 2 or 3 or 4.THANK YOU!!!
:
: If you know that the function always takes 2 or 3 or 4 parameters
: ( This is what you say )
:
: You can also use overloaded functions:
:
: [code]
: function add(s1, s2:string):string; overload;
: begin
: add := s1 + s2;
: end;
:
: function add(s1, s2, s3:string):string; overload;
: begin
: add := s1 + s2 + s3;
: end;
:
: function add(s1, s2, s3, s4:string):string; overload;
: begin
: add := s1 + s2 + s3 + s4;
: end;
: [/code]
:
: I wanted to write a program like concat
: Concat can support many parameters or 2 parameters
Well, then you could write 255 different overloaded functions. :-o
Or you can use '[' and ']', like any other pascal programmer would do.
Or you can read the FreePascal sources and
find out how concat() is implemented...
But I have find out most of compiler
I also cannot find the source
Can you give me??
: : But I do not know how many parameters
: : I wanted to write a program like concat
: : Concat can support many parameters or 2 parameters
:
:
: Well, then you could write 255 different overloaded functions. :-o
:
: Or you can use '[' and ']', like any other pascal programmer would do.
:
: Or you can read the FreePascal sources and
: find out how concat() is implemented...
: