Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to find the max absolute difference between consecutive characters in a string with Pascal

2 Answers

0 votes
program MaxAsciiDiff;

function MaxAsciiDiff(const s: string): Integer;
var
  i, diff, maxDiff: Integer;
begin
  if Length(s) < 2 then
    Exit(0);

  maxDiff := 0;

  for i := 1 to Length(s) - 1 do
  begin
    diff := Abs(Ord(s[i]) - Ord(s[i + 1]));
    if diff > maxDiff then
      maxDiff := diff;
  end;

  MaxAsciiDiff := maxDiff;
end;

var
  s: string;
begin
  s := 'jumplings';
  WriteLn('Maximum ASCII difference: ', MaxAsciiDiff(s));
end.




(*
run:

Maximum ASCII difference: 12

*)


 



answered Jan 9 by avibootz
0 votes
program MaxAsciiDiff;

function MaxAsciiDiff(const s: string; var c1, c2: char): Integer;
var
  i, diff, maxDiff: Integer;
begin
  if Length(s) < 2 then
  begin
    MaxAsciiDiff := 0;
    Exit;
  end;

  maxDiff := 0;

  for i := 1 to Length(s) - 1 do
  begin
    diff := Abs(Ord(s[i]) - Ord(s[i + 1]));
    if diff > maxDiff then
    begin
      maxDiff := diff;
      c1 := s[i];
      c2 := s[i + 1];
    end;
  end;

  MaxAsciiDiff := maxDiff;
end;

var
  s: string;
  a, b: char;
  resultValue: Integer;

begin
  s := 'jumplings';
  a := #0;
  b := #0;

  resultValue := MaxAsciiDiff(s, a, b);

  WriteLn('Maximum ASCII difference: ', resultValue);
  WriteLn('Characters: ''', a, ''' and ''', b, '''');
end.



(*
run:

Maximum ASCII difference: 12
Characters: 'g' and 's'

*)

 



answered Jan 9 by avibootz

Related questions

1 answer 82 views
...