Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

concat problem

wingwingkcwwingwingkcw Posts: 37Member
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.
«1

Comments

  • PerranPerran Posts: 241Member
    : 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.
  • wingwingkcwwingwingkcw Posts: 37Member
    Sorry!!I wanted write a program is that:
    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.
    :

  • zibadianzibadian Posts: 6,349Member
    : Sorry!!I wanted write a program is that:
    : 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]
  • Jeff P.Jeff P. Posts: 196Member
    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.
    :
  • wingwingkcwwingwingkcw Posts: 37Member
    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.
    : :
    :

  • zibadianzibadian Posts: 6,349Member
    I've not much experience with FreePascal, but if you are trying to pass a dynamic array to a procedure, you'll need to change the calling code to:
    [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.
    : : :
    : :
    :
    :

  • Jeff P.Jeff P. Posts: 196Member
    : 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.
  • wingwingkcwwingwingkcw Posts: 37Member
    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.
    :

  • zibadianzibadian Posts: 6,349Member
    In most Pascals (TP & Delphi I'm sure) you need to rebuild the compiler itself for that (See my post of 2003-5-31 for details).

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

  • Jeff P.Jeff P. Posts: 196Member
    : 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]
«1
Sign In or Register to comment.