Good day, I have made this code that is supposed to count distinct characters in a string, my code was tested by several inputs but it failed to count characters in this input :
zcinitufxoldnokacdvtmdohsfdjepyfioyvclhmujiqwvmudbfjzxjfqqxjmoiyxrfsbvseawwoyynn
it has almost 80 characters and the maximum string that Pascal can read has the length of 256 character. I couldn't find a better algorithm to solve this, so I'm looking for some help from the experts in this field, or anyone that is open to share knowledge.
I guess that my code is jumping a character in each loop.
Here is my code :
function freq(char: char; username : String): Integer;
var
i, auxfreq: Integer;
begin
auxfreq:= 0;
for i:= 1 to length(username) do
if char = username[i] then
auxfreq:= auxfreq + 1;
//writeln(freq);
freq:= auxfreq;
end;
function OddUserName(username : String): Boolean;
var
usernameaux : String;
length_usernameaux, i : Integer;
Result : Boolean;
begin
Result:= false;
usernameaux:= username;
i:= 0;
repeat
i +=1;
length_usernameaux:= length(usernameaux);
if freq(usernameaux[i], usernameaux) <> 1 then
delete(usernameaux, i, 1);
until i = length_usernameaux;
// length(usernameaux) is supposed to be the number of the distinct characters.
{if length(usernameaux) mod 2 <> 0 then // you will have to ignore this.
Result:= true; // odd}
//writeln(usernameaux);
//writeln(length(usernameaux));
OddUserName:= Result; // ignore this too
end;
I sincerely appreciate your help.
ShortString, not forstring. - Remy Lebeaudelete()'ing characters from a string would affect your loops, skipping characters. You haven't fixed that properly yet. - Remy Lebeau