Markski
Markski

Reputation: 21

Count how often a character appears in a string

Before someone asks, no, this is not homework (I can understand someone assuming it since it's a rather silly question), I'm just preparing myself for pascal since I will see it in a soon course and I'm putting myself some little chalenges and see what I can do about them, but I am most absolutely puzzled about this one.

Let's say I have a string of many numbers, let's say 2437341323 , and I would like to count those 3's.

For example, the number 3 appears 4 times in that string of numbers, so I'd like an output that is 4, so I can say "The number 3 shows up 4 times in this string".

How can I do this?

Excuse my bad english and thanks for your time reading this post, and if possible, answering it.

Upvotes: 1

Views: 196

Answers (3)

Dominic
Dominic

Reputation: 38

Traverse the string, being it that string is an array of char, and use an if statement to check for the appropriate character. In my example the character we are looking for is provided when the function is called.

function checkforchar(s:string;c:char):integer;
var
  i:integer;
begin
  checkforchar:=0;
  for i:=1 to length(s) do
    if s[i]=c then inc(checkforchar);
end;

When loops aren't provided with begin and end statements, considering if and case statements as well, they only run the next line of code. Keep in mind here that the stuff between a begin and an end; inclusive, a block of code, is picked up entirely by the loop or statement as if one line which is why this works.

--EDIT--

Here is a usage example.

fourspresent:=checkforchar(stringexample,'4');

If you would like to look for an entire string in the other string you can do as follows.

function checkforstring(s,s2:string):integer; {where s must be bigger than s2}
var
  i,e:integer;
  patched_s:string;
begin
  checkforstring:=0;
  for i:=1 to length(s)-length(s2)+1 do
  begin
    patched_s:='';

    for e:=i to i+length(s2)-1 do
      patched_s:=patched_s+s[e];

    if patched_s=s2 then inc(checkforstring);
  end;
end;

Upvotes: -1

Helen Fairgrieve
Helen Fairgrieve

Reputation: 97

Walk the string as an array from 1..length(mystring) then check each character as mstring[element]='3' Using the following procedure you can check for matches of more than just one characters so (this is for 2 byte strings - untested in older versions). Important info : when referencing strings as an array - remember that strings begin from element [1] rather than [0] as in most of the rest of pascal's default structures & classes.

For x:=1 to length(mystring) do
begin
     if IsSubStringAtPos(mystring,x,'333',True) then inc(MatchCount);
end;
Function IsSubstringAt(source:String;atPos:integer;Mask:String;CaseSensitive:boolean):Boolean;overload;
var
    SourceP,MaskP:PChar;
    sourceL,maskl:integer;
    i:integer;
begin
   result:=false;
   if source='' then exit;
   if mask='' then exit;
   if atpos<1 then exit;

   SourceL:=Length(Source);
   MaskL:=Length(mask);
   if atpos>SourceL-maskL+1 then exit;

   SourceP:=@Source[atpos];
   MaskP:=@Mask[1] ;

   result:=true; //now we can only fail and set false;
   for i:=1 to maskL do
   begin
        case CaseSensitive of
        True : Begin
                    if sourcep^<>maskp^ then
                    begin
                        result:=false;
                        break;
                    end;
                    inc(sourcep);
                    inc(maskp);
               end;
        False:Begin
                   if AnsiUpperCase(SourceP^)<>ansiuppercase(Maskp^) then
                   begin
                        result:=false;
                        break;
                   end;
                   inc(sourceP);
                   inc(maskP);
              end;
        end;//of case
   end;

Upvotes: 0

Ken White
Ken White

Reputation: 125620

In Pascal, you can treat a string like a 1-based array of characters, so you can simply iterate through the string, counting the characters you want to count:

function CountChar(const Ch: Char; const Str: string): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(Str) do
    if Str[i] = Ch then
      Inc(Result);
end;

Sample use:

NumThrees = CountChar('3', '2437341323');

For older versions of Pascal that don't provide an automatic Result variable, declare Result as a variable local to the procedure and simply return it:

function CountChar(const Ch: Char; const Str: string): Integer;
var
  i, Result: Integer;
begin
  Result := 0;
  for i := 1 to Length(Str) do
    if Str[i] = Ch then
      Inc(Result);
  CountChar := Result;
end;

Upvotes: 3

Related Questions