Board index » delphi » Checking an string for a char (bear with me)

Checking an string for a char (bear with me)

This is an utter (very) newbie question: I need a procedure or
function that can check a string (that grows in length for each round
in a loop) for a particular character, and returns a boolean (true if
the char is found in the string, false if it isn't). I suppose I can
do this with some for..in...do-loop, but I wonder if there is some
predefined function or procedure in the (Free Pascal) units that can
do this a bit more elegant, or some code snippet that would do the
job. I have browsed the units documentation some, without finding
anything very suitable.

So the point is that the string should only contain different
characters, not any two of the same kind.

?ystein Skundberg

--
Blessed are they who can laugh at themselves
for they shall never cease to be amused.
                                *       http://home.chello.no/~yskundbe/
yskun...@chello.no                    http://www.unamerican.com

 

Re:Checking an string for a char (bear with me)


On Tue, 10 Jul 2001 22:15:39 GMT, yskun...@chello.no (?ystein

Quote
Skundberg) wrote:

>This is an utter (very) newbie question: I need a procedure or
>function that can check a string (that grows in length for each round
>in a loop) for a particular character, and returns a boolean (true if
>the char is found in the string, false if it isn't). I suppose I can
>do this with some for..in...do-loop, but I wonder if there is some
>predefined function or procedure in the (Free Pascal) units that can
>do this a bit more elegant, or some code snippet that would do the
>job. I have browsed the units documentation some, without finding
>anything very suitable.

>So the point is that the string should only contain different
>characters, not any two of the same kind.

>?ystein Skundberg

Program RandomString;   {80 different chars}
{NOT FPC but pretty standard.}

VAR
s:String;
ct:Byte;
ch:Char;
present:Boolean;

Begin
     Randomize;    s := '';    ct := 0;
     Repeat
          ch := Chr(Random(127-32) + 32);  {chars ' '..'~'}
          present := Pos(ch, s) > 0;
          If not present then
          Begin
               Inc(ct);
               s := s + ch;
          End;
     Until ct = 80;
     Writeln(s); readln;
End.

Re:Checking an string for a char (bear with me)


Quote
Clif Penn <clifp...@airmail.net> writes:

<snip question: is there a way to make sure the same character doesn't
appear twice in a string?>

Quote

> Program RandomString;   {80 different chars}
> {NOT FPC but pretty standard.}

> VAR
> s:String;
> ct:Byte;
> ch:Char;
> present:Boolean;

> Begin
>      Randomize;    s := '';    ct := 0;
>      Repeat
>           ch := Chr(Random(127-32) + 32);  {chars ' '..'~'}
>           present := Pos(ch, s) > 0;
>           If not present then
>           Begin
>                Inc(ct);
>                s := s + ch;
>           End;
>      Until ct = 80;
>      Writeln(s); readln;
> End.

Thank you very much, this looks like a more sensible approach to the
problem. :)

?S

--
Blessed are they who can laugh at themselves
for they shall never cease to be amused.
                                *       http://home.chello.no/~yskundbe/
yskun...@chello.no                    http://www.unamerican.com

Re:Checking an string for a char (bear with me)


JRS:  In article <52FB9BFB446278D4.541CA6470E12074B.33E544F47831D...@lp.a
irnews.net>, seen in news:comp.lang.pascal.misc, Clif Penn
<clifp...@airmail.net> wrote at Tue, 10 Jul 2001 17:49:36 :-

Quote
>On Tue, 10 Jul 2001 22:15:39 GMT, yskun...@chello.no (?ystein
>Skundberg) wrote:
>> ...
>>So the point is that the string should only contain different
>>characters, not any two of the same kind.

Any method involving choosing at random and scanning to see if already
used will become more and more inefficient as the number of remaining
possibilities diminishes.

ISTM that the best way is to generate a string of the maximum possible
length, N, with all characters allowable and different, then to use this
as a source.

Generating the string of length N is basically equivalent to dealing
numbers 0..(N-1); see
        <URL: http://www.merlyn.demon.co.uk/pas-rand.htm#Deal>
which has, for cards

    for J := 1 to 52 do begin
     K := Random(J)+1 ; A[J] := A[K] ; A[K] := J end ;

If your N allowable characters are X to Y, then perhaps

   A[0] := N ;
   for J := 1 to N do begin
     K := Random(J)+1 ; A[J] := A[K] ; A[K] := char(Pred(J)+Ord(X)) end ;

which will need adjustment of strings are not stored as in Turbo Pascal.

See also Rifkind, /loc. cit./

If the questioner is really using TP or BP, then
news:comp.lang.pascal.borland should be used.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Other Threads