Domanda

My task is to show most used letter in a row. For example if you put in aabbbbccbbb most repeated character is B and it is used 4 times. There was a very similar topic about the same task, but i didnt understand the code. Most repeating character in a string

     Program Task;
var s:string;
    i,k,g,count:integer;
    c:char;
begin
  Readln(s);
  g:=0;

  while Length(s) > 0 do
  begin
    c := s[1];
    i:=1;
    while i<= Length(s) do
    begin
    If (c=s[i]) then
    delete(s,i,1)
     else
    Inc(i);
    If (c=s[i]) then
    Inc(g);
    end;
  end;

  Writeln(g);

  Readln;
end.

There are many problems i face. First is i dont know how to show which character is most used and second is i dont know how compare which one of repeating characters is most used. For example if i write aaaabbbc it will give me answer of 7 because there is 4xa and 3xb. All the help is most appreciated.

È stato utile?

Soluzione

If it's just about english characters, you might just allocate an array to keep a count per character. In that case, the code could look like this.

I wrote this using Delphi. I hope it works as well in your flavour of Pascal.

program Task;

{$APPTYPE CONSOLE} // For Delphi

var
  s: string[50];
  i: Integer;
  Counters: array[Char] of Integer;
  Highest: Char;
begin
  // Initialize counters.
  for i := 0 to 255 do
    Counters[Char(i)] := 0;

  s := 'aabbbbccbbb';

  // Count the characters.
  for i := 1 to Length(s) do
    Inc(Counters[s[i]]);

  // Find out which one is highest.
  Highest := #0;
  for i := 0 to 255 do
    if Counters[Char(i)] > Counters[Highest] then
      Highest := Char(i);

  // Output that character and its count.
  WriteLn('The highest character is ', Highest, ' with ', Counters[Highest], ' occurrences.');
  ReadLn;
end.

In less academic setups, using an array like this might not be the most efficient, because it contains a counter for every possible character, including those that don't occur in the string at all. That means, if you want to use this exact code for every possible character in the unicode table, your array would be a couple of megabytes large (still not really a problem on modern computers, but still).

You can improve this code by using a kind of dictionary or list to keep track of the items, so you need only to add those items you find, but if you have to write that yourself, it will make your program quite a bit larger.

EDIT:

As per request in comment: Counting the longest subsequent range of characters:

program Task;

{$APPTYPE CONSOLE} // For Delphi

var
  s: String;
  i: Integer;
  Longest: Integer;
  Current: Integer;
  LongestChar: Char;
begin
  s := 'aabbbbccbbb';

  Longest := 0;
  Current := 0;
  // Count the characters.
  for i := 1 to Length(s) do
  begin
    Inc(Current);
    // If it's the last char or the next char is going to be different, restart the counting.
    if (i = Length(s)) or (s[i] <> s[i+1]) then
    begin
      if Current > Longest then
      begin
        Longest := Current;
        LongestChar := s[i];
      end;
      Current := 0;
    end;
  end;

  // Output that character and its count.
  WriteLn('The highest character is ', LongestChar, ' with ', Longest, ' occurrences.');
  ReadLn;
end.

Current > Longest makes sure the first longest sequence is returned in case multiple character sequences have the same length. Change to Current >= Longest if you want the last sequence instead.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top