计算字符在字符串中出现的频率

时间:2016-04-20 20:24:56

标签: string numbers pascal

在某人问之前,不,这不是家庭作业(我可以理解有人假装,因为这是一个相当愚蠢的问题),我只是为帕斯卡做准备,因为我会在很快的过程中看到它并且我放我自己有些挑战,看看我能对他们做些什么,但我对这一点感到非常困惑。

假设我有一串很多数字,比方说2437341323,我想算一下这些数字。

例如,数字3在该字符串中出现4次,所以我想输出为4,所以我可以说“数字3在这个字符串中显示4次​​”。

我该怎么做?

请原谅我的英文不好,感谢你抽出时间阅读这篇文章,如有可能,请回答。

3 个答案:

答案 0 :(得分:3)

在Pascal中,您可以将字符串视为基于1的字符数组,因此您可以简单地遍历字符串,计算要计算的字符数:

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;

样品使用:

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

对于不提供自动Result变量的旧版Pascal,请将Result声明为过程的本地变量,然后只返回它:

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;

答案 1 :(得分:0)

将字符串作为数组从1..length(mystring)中移出,然后将每个字符检查为mstring [element] =' 3'使用以下过程可以检查多于一个字符的匹配(这是2字节字符串 - 在旧版本中未经测试)。重要信息:将字符串引用为数组时 - 请记住,字符串从元素[1]而不是[0]开始,就像在其余大多数pascal的默认结构中一样。类。

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;

答案 2 :(得分:-1)

遍历字符串,因为stringarray of char,并使用if语句检查相应的字符。在我的例子中,调用函数时会提供我们要查找的字符。

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;

当循环没有提供开始和结束语句时,考虑到ifcase语句,它们只运行下一行代码。请记住,beginend;之间的内容(一个代码块)之间的内容完全由循环或语句获取,就好像一条线就是这样的原因。

- 编辑 -

这是一个用法示例。

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

如果您想在另一个字符串中查找整个字符串,可以执行以下操作。

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;