ReallyQuerdey
ReallyQuerdey

Reputation: 21

How to count number of occurrences of a character in an array in Pascal

I have to script a pascal code that rations into calculation the frequency of a character's appearance in the code and displays it through the output mode

Input P2 changes:

Second Attempt at the coding phase I tried revisioning the code.I added the output variable writeln('input array of characters'); & writeln('Number of Occurrences',k);, which should help me output how many times did the S character appear overall in the code, plus utilised the for & if commands to have the final values showcased based on the conditions, if the frequency is 1 then count in S, still getting errors, take a look at the Input P2 & Output P2

Input P1
function Count(t, s: String): Integer;
var
  Offset, P: Integer;
begin
  Result := 0;
  Offset := 1;
  P := PosEx(t, s, Offset);
  
  while P > 0 do
  begin
    Inc(Result);
    P := PosEx(t, s, P + 1);
  end;
end;

Output P2
Target OS: Linux for x86-64
Compiling main.pas
main.pas(5,3) Error: Identifier not found "Result"
main.pas(7,8) Error: Identifier not found "PosEx"
main.pas(8,3) Error: Identifier not found "unsigned"
main.pas(8,12) Fatal: Syntax error, ";" expected but "identifier N" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode

-------------------------------------------------------------------

Input P2
program p1

var S:string

i:integer
begin
writeln('input array of characters');
k:=O;
for i:=1 to length (S) do
if (S[i])='m') and (S[i+1]='a') then k:=k+1;
writeln('Number of  Occurrences',k);
Readln;
end.

Output P2
Compiling main.pas
main.pas(2,1) Fatal: Syntax error, ";" expected but "VAR" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode

Upvotes: 1

Views: 1027

Answers (2)

Paul Robinson
Paul Robinson

Reputation: 1

I think this is more like what you want to do:

function Count(t, s: String): Integer;
var
  Offset,Res, P: Integer;
begin
  Res := 0
  Offset := 1;
  repeat
       P := Pos(t, s, Offset);
       if p>0 then 
          Inc(Res);
       Offset := P+1
  untl P = 0;
  Count := Res; 
end;

Now, if you don't have Pos, you can implement it:

Function Pos(const t,s:string; const Start:integer):Integer;
Var
    LS, LT,     {Length}
    IxS, IxT,   {Index)
    R: Integer; {Result}

begin
    R := 0;
{use only one of the two following lines of code}
{if your compiler has length}
    LS := length(S); LT := Length(T);
{If it does not}
    LS := Ord(s[0]); LT := Ord(T[0]);

    if (LS <= LT)  {if target is larger than search string, it's not there}
      and (Start<=LT) and {same if starting position beyond size of S}
       (Start+LT <-LS)  then  {same if search would go beyond size of S} 
    begin  {Otherwise, start the search}
        ixT := 1;
        ixS := Start;
        repeat 
             Inc(R); {or R:= R+1; if INC not available }
             If (S[ixS] <> T[ixT]) then
                  R := 0  {they don't match, we're done}
             else
             begin {Move to next char}
                 Inc(ixS);
                 Inc(ixT);
             end;
        until (R=0) or (ixT>LT); {if search failed or end of target, done}
        Pos := R;
    end;

Upvotes: 0

Tom Brunberg
Tom Brunberg

Reputation: 21033

The errors you see in the first block:

Identifier not found "Result"

Standard Pascal doesn't recognize the pseudovariable Result. In some Pascal implementations (like e.g. Delphi) it can be used to assign a value to the function result. The Pascal you are using needs to have the result of a function assigned to the name of the function. For example:

function Whatever(): integer;
begin
  Whatever := 234;
end;

Identifier not found "PosEx"

Not all Pascal implementations include the PosEx() function. You need to use Pos() instead. But, the standard implementation of Pos() doesn't include the "search start position" that PosEx has. Therefore you need to ditch Pos() and do as you do in "Input P2", that is traverse the text character per character and count the occurances as you go.

Identifier not found "unsigned"

Seems you have removed that unknown identifier.

The error you see in the second block:

In Output P2 the error message should be clear. You are missing a semicolon where one is needed. Actually you are missing three of them.

You are also missing the line that reads user input: ReadLn(S);.

Finally, to calculate both upper and lower case characters you can use an extra string variable, say SU: string to which you assign SU := UpperCase(S) after reading user input, and then use that string to count the occurances.

Upvotes: 1

Related Questions