Search code examples
stringpascallazarusfreepascal

How to repeat string in freepascal


i need help with this problem that i have. I need to repeat a certain string multiple times. For example string 'hello' should be repeated 3 times with output like this: hellohellohello

What I need to do is have user input an integer and after that another one(n). The program will repeat the first integer n-times. For example: 11 3: 111111

And that output should be integer ready to be compared to another integer. So I tought that I have to input that as a integer, convert it to string, repeat the string n-times and put that new string to integer variable and than compare it to what I need to.

Any suggestions?

EDIT: Another problem occured: apparently my code cannot use any uses. So how can it be done withou them?


Solution

  • You can use the DupeString function in the StrUtils unit for this:

    program duplicatestring;
    
    {$mode objfpc}{$H+}
    
    uses
      Classes, SysUtils, StrUtils
      { you can add units after this };
    
    begin
      writeln(DupeString('hello', 5));
      Readln;
    end.
    

    Obviously, I've hard-coded the string'hello' and 5 repeats in this example, but you could get the string and repeat count from user input or anywhere you like. And you could write your own string function to generate N repeats of a given string by internally calling DupeString.

    I suggest you look at DupeString's souce code to see how this is implemented and then experiment with other methods of doing the same thing, e.g. using a for, while or repeat untiil loop.

    Re your "no USES" update, the source code of `DupeString' shows one way to do this:

    Function DupeString(const AText: string; ACount: Integer): string;
    var i,l : SizeInt;
    begin
     result:='';
     if aCount>=0 then
       begin
         l:=length(atext);
         SetLength(result,aCount*l);
         for i:=0 to ACount-1 do
           move(atext[1],Result[l*i+1],l);
       end;
    end;
    

    Basically, the SetLength allocates enough room on the heap for ACount copies of AText, then the move copies AText contents into the allocated space ACount times, and the function result tells its caller where to find the contents of the heap allocation (because of the H+ compiler directive - the result is returned differently if using H-, because that makes the compiler use old-style, length-byte-prefixed Pascal strings).

    Another way is

    function MyDupeString(const AText: string; ACount: Integer): string;
    var i: SizeInt;
    begin
     result:='';
     for i := 1 to ACount do
       Result := Result + AText;
    end;
    

    which is a lot less code, but a lot less efficient, because each iteration of the for loop causes a new heap allocation for the function result and a deallocation of the previous one.

    And anything you can so with a Pascal for loop, you can do with a while .. do or repeat until one. Challenge: See if you can write your own MyDupeString which uses a while loop, and submit that as your answer to whoever has set you this task.