fork download
  1. program ideone;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. function MultiCharsCountAsInteger(AInput: array of AnsiChar): Integer;
  6. const
  7. COUNTED_CHAR_VALUE = AnsiChar(#0);
  8. var
  9. intCharToken, intCounterToken, intCharCount: Integer;
  10. begin
  11. Result := 0;
  12.  
  13. for intCharToken := Low(AInput) to High(AInput) do
  14. if AInput[intCharToken] <> COUNTED_CHAR_VALUE then
  15. begin
  16. intCharCount := 1;
  17.  
  18. for intCounterToken := intCharToken + 1 to High(AInput) do
  19. if AInput[intCharToken] = AInput[intCounterToken] then
  20. begin
  21. Inc(intCharCount);
  22. AInput[intCounterToken] := COUNTED_CHAR_VALUE;
  23. end;
  24.  
  25. if intCharCount > 1 then
  26. Inc(Result);
  27. end;
  28. end;
  29.  
  30. function MultiCharsCountAsString(AInput: array of AnsiChar): AnsiString;
  31. const
  32. COUNTED_CHAR_VALUE = AnsiChar(#0);
  33. var
  34. intCharToken, intCounterToken, intCharCount: Integer;
  35. begin
  36. Result := '';
  37.  
  38. for intCharToken := Low(AInput) to High(AInput) do
  39. if AInput[intCharToken] <> COUNTED_CHAR_VALUE then
  40. begin
  41. intCharCount := 1;
  42.  
  43. for intCounterToken := intCharToken + 1 to High(AInput) do
  44. if AInput[intCharToken] = AInput[intCounterToken] then
  45. begin
  46. Inc(intCharCount);
  47. AInput[intCounterToken] := COUNTED_CHAR_VALUE;
  48. end;
  49.  
  50. if intCharCount > 1 then
  51. Result += AInput[intCharToken];
  52. end;
  53. end;
  54.  
  55. const
  56. STRING_TO_TEST = AnsiString('aabbcdaaebbfgaah');
  57. begin
  58. WriteLn('As Integer: ', MultiCharsCountAsInteger(STRING_TO_TEST));
  59. WriteLn('As String: ', MultiCharsCountAsString(STRING_TO_TEST));
  60. end.
  61.  
Success #stdin #stdout 0s 280KB
stdin
Standard input is empty
stdout
As Integer: 2
As String:  ab