for-loopfreepascalstring-searchord

Writing a simple function which returns True if there is a duplicate character present


So basically as in the title. Here is what I am trying to do:

3 
 24 function RepeatedChar (l: char; s: string): boolean;
 25 var
 26 c, ordl, ords: integer; {counter}
 27 begin
 28     c := 0;
 29     writeln(' LAUNCHED SEARCH FOR DUPLICATES ');
 30     writeln('________________________________');
 31     writeln('Searching for duplicates in: ', s);
 32     writeln('char: ', l);
 33     writeln('length: ', length(s));
 34     for c := 1 to length(s) do
 35         begin
 36             writeln(ord(s[c]));
 37             ordl := ord(l);
 38             ords := ord(s[c]);
 39             writeln('MATCHING: ', ordl, ' with ', ords);
 40             if ordl = ords then
 41                 RepeatedChar := true
 42             else
 43                 RepeatedChar := false;
 44         end;
 45     writeln('________________________________');
 46     writeln(' ALL DONE ');
 47 end;

But the function won't behave correctly and here is one of the examples:

 LAUNCHED SEARCH FOR DUPLICATES 
________________________________
Searching for duplicates in: umpty
char: t
length: 5
117
MATCHING: 116 with 117
109
MATCHING: 116 with 109
112
MATCHING: 116 with 112
116
**MATCHING: 116 with 116**
121
MATCHING: 116 with 121
________________________________
 ALL DONE 
Returned False

While here, all is good (note: it's the same string being searched):

 LAUNCHED SEARCH FOR DUPLICATES 
________________________________
Searching for duplicates in: umptyta
char: a
length: 7
117
MATCHING: 97 with 117
109
MATCHING: 97 with 109
112
MATCHING: 97 with 112
116
MATCHING: 97 with 116
121
MATCHING: 97 with 121
116
MATCHING: 97 with 116
97
MATCHING: 97 with 97
________________________________
 ALL DONE 
Returned True

Please help, I feel like I am going insane and my code turns into more spaghetti with each iteration.

Function is expected to return True when results of ord() match. But it happens only in some cases.


Solution

  • The reason why your function returns false in your first example is the fact that while in 4th loop cycle the result is set to true it is set back to false in the 5th loop.

    So in order to avoid this you can initialize the result to false and then change it to true if duplicate is found as Tom Brunberg already recommended in his comment.

    Or even better you go and exit the current loop using Break command when specific character is found so your program isn't unnecessarily checking the rest of the string.


    Also since you are searching for a presence of a specific character in a string you might want to make use of Pos function which allows of searching for a substring within a string. Yes the searched string can be a single character.

    If Pos finds the searched substring within a string it returns the position of the searched substring. If the substring can't be found then it returns 0.

    So your whole procedure can be as simple as this:

    function RepeatedChar (l: char; s: string): boolean;
    begin
      if Pos(l,s) > 0 then RepeatedChar := True
      else RepeatedChar := False;
    end;