Finding the least frequent char in a string

Hey!

I need some help with my pascal homework. What I need to do, is make the program return the least frequent character(s) in a string and number of occurances.
1) User inputs the main string, which will be searched for specified characters;
2) User inputs second string (5 characters long) with the characters to be searched in the main string;
3) Program returns the result. If no characters were found, output appropriate message; if more than 1 character is the least frequent, display both as a result.

Must have a module and main program to use it.

Here is what I have so far (does not work quite the way I want it to):
[b]Program:[/b]
[code]
program labd3;
uses labd3u,crt;

var
InputString: StrMod;
Letters: StrData;
Choice: string;
label BEGINNING, END, INPUT1, INPUT2;
begin
BEGINNING:
clrscr;
InputString:='';
Letters:='';
readln;
INPUT1:
writeln('Input the string to analyze.');
readln(InputString);
if InputString='' then
begin
writeln('Your string is empty! Input a new string.');
goto INPUT1
end
else
INPUT2:
writeln('Input 5 symbols to search.');
readln(Letters);
if Letters='' then
begin
writeln('You must enter at least 1 symbol, please re-enter');
goto INPUT2
end
else
writeln('--------------------------------------------------------------');
CharCount (InputString, Letters);
if x=0 then
begin
writeln('One or more symbols were not found.');
end
else
begin
writeln('In the string "',InputString,'" the least frequent symbol is "',res,'" (',x,' times).');
end;
writeln('--------------------------------------------------------------');
writeln('Type 1 to input new strings.');
writeln('Type anything else to quit.');
read(Choice);
if Choice='1' then goto BEGINNING else goto END;
readln;
readln;
END:
end.[/code]

[b]Module:[/b]

[code]unit labd3u;
Interface
const
LenStrMod=255;
LenStrData=5;

type StrMod = string [LenStrMod];
StrData = string [LenStrData];

var x: integer;
res: string;
procedure CharCount ( InpStr: StrMod; Symbols: StrData);

implementation
var
i,j,n,t: integer;
lenv,lens: integer;
temp,letter: string;
procedure CharCount ( InpStr: StrMod; Symbols: StrData);
begin
writeln('String To analyze: "' ,InpStr, '".');
writeln('Characters to search for: "' ,Symbols, '".');
x:=50;
n:=0;
i:=1;
lenv:=length(InpStr);
lens:=length(Symbols);
repeat
for j:=1 to lenv do
begin
if symbols[i]=InpStr[j] then
begin
if Symbols[i]<>temp then
begin
t:=n;
letter:=temp;
n:=0;
end;
n:=n+1;
{writeln(n);}
temp:=Symbols[i];
end;
end;
i:=i+1;
if n<=t then
begin
t:=n;
letter:=temp;
end;

until i=lens+1;
x:=t;
res:=letter;
end;
end.[/code]


There may be mistakes as I had to translate the program to english.

Comments

  • Okay, here is the flow:

    First change all chars to either UPPER or lower cases.

    FOR i:= 1 TO 5 DO Frequency[i]:= 0;

    Let's say you apply lower cases, search for the 5 (unique) chars and record the frequencies using FOR y:= 1 TO 5 DO FOR x:= 1 TO length(string) DO IF string[x]=char[y] THEN frequency[y]+1 statement.

    Declare the least frequent char as "min" var. Use IF frequency0 THEN list out all chars with frequencies equal to the least; ELSE print sth like 'No match'.

    **** I think you have to suppose all 5 chars must all unique.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion