# Board index » delphi » Pascal "Word" Problem

## Pascal "Word" Problem

Hello Everyone,

As all of you know, the 'Chr' function returns an uppercase letter for
integers between 65 and 90, inclusive. (ASCII code)  I must write a program
to randomly generate and print six letter "words", each of which contains at
least one vowel.  I must discard (do not print) words without vowels, and
continue until five words have been printed:

This is what I have so far:

-----------------------------------------------------------------------------
Program arrchr;
uses crt;
var
list:array[1..6]of integer;
num:integer; {random numbers which make up the "words"}
outer:integer; {loop control variable - writes each line}
inner:integer; {loop control variable - writes the numbers in each line}

begin {arrchr}
clrscr;
randomize;

for outer:=1 to 5 do
begin
write('Word ',outer,': ');
for inner:=1 to 6 do
begin
num:=random(25)+65;
list[inner]:=num;
write(chr(list[inner]),' ');
end;
writeln;
end;

end. {arrchr}

------------------------------------------------------------------------------
------

The problem is, I need to know how to discard the "words" that do not contain
vowels.  If any of you can help me, mail me at xmo...@aol.com.

I look forward to hearing from you.

- X :-]

## Re:Pascal "Word" Problem

##### Quote
>The problem is, I need to know how to discard the "words" that do not contain
>vowels.  If any of you can help me, mail me at xmo...@aol.com.

Am I missing something?  [I probably am, as I recently called the Pascal
arctan function both "atan" and "arctangent" in postings ...].  You've done
the hard part, generating the 6-letter words.  Now examine each letter and see
if it is a vowel ('A', 'E', 'I', 'O', 'U') (I confess I don't know what to do
with "and sometimes 'Y'" ...).

Bob Schor
Pascal Enthusiast

## Re:Pascal "Word" Problem

I'm not going to solve the problem completely, because I haven't got
access to my compiler right now, so I can't test any code.  But, I
can tell you the approach I'd use.

Declare a set of type char which contains the vowels
- var
-   vowels : set of char;
-
- vowels := [ 'A', 'E', 'I', 'O', 'U' ]

and declare yourself a boolean, ContainsAVowel, which you initialise to FALSE.
Write the random letters to an array of char, testing each one as you go along
to see if it is a vowel.  If it is (i.e., it's a memeber of the vowels
set) then set ContainsAVowel to true.  Once you've done all six letters,
if ContainsAVowel is true, then write out the word and increment the outer
counter.

I've changed the code you included.  This may not work at first crack,
as I can't test it, but the idea is sound enough.

Enjoy!

ChrisR:

##### Quote
Xmo...@aol.com wrote:
>Hello Everyone,

>-----------------------------------------------------------------------------
>Program arrchr;
>uses crt;
>var

list:array[1..6]of char;
##### Quote
>   num:integer; {random numbers which make up the "words"}
>   outer:integer; {loop control variable - writes each line}
>   inner:integer; {loop control variable - writes the numbers in each line}

vowel: set of char;
ContainsAVowel:boolean;
##### Quote
>begin {arrchr}
>     clrscr;

vowel := [ 'A', 'E', 'I', 'O', 'U'];
##### Quote
>     randomize;

outer := 1;
while (outer <= 5) do
##### Quote
>         begin

ContainsAVowel := FALSE;
##### Quote
>              for inner:=1 to 6 do
>                  begin
>                       num:=random(25)+65;
>                       list[inner]:=chr(num);

ContainsAVowel := ((list[inner] in vowels) or
ContainsAVowel);
##### Quote
>                  end;

if ContainsAVowel then
begin
write( 'Word', outer, ': ' );
writeln( list );
outer := outer + 1;
ContainsAVowel := FALSE;
end;
##### Quote
>         end;

>end. {arrchr}

>------------------------------------------------------------------------------
>------

>The problem is, I need to know how to discard the "words" that do not contain
>vowels.  If any of you can help me, mail me at xmo...@aol.com.

>I look forward to hearing from you.

>- X :-]

## Re:Pascal "Word" Problem

##### Quote
On Wed, 31 Jan 1996 05:09:56 GMT, Xmo...@aol.com wrote:
>As all of you know, the 'Chr' function returns an uppercase letter for
>integers between 65 and 90, inclusive. (ASCII code)  I must write a program

Actually, Chr() returns a 'char' type (unsigned byte) between 0 and 255
inclusive, but who's counting.... <g>

##### Quote
>This is what I have so far:

>-----------------------------------------------------------------------------
>Program arrchr;
>uses crt;
>var
>   list:array[1..6]of integer;

This is an array of six integers, two bytes apiece.  Since you're
creating words of text, you may as well use the 'char' type directly
instead of converting at the end.

##### Quote
>The problem is, I need to know how to discard the "words" that do not contain
>vowels.  If any of you can help me, mail me at xmo...@aol.com.

Turn the problem around.  Instead of printing out the words as they are
created, examine them *before* you display them.  Use a string array to
hold the words which pass your critera, and then simply print them all
out at the end.

For example;

Program RandomWords;

const NUMWORDS = 5;
WORDLEN  = 6;
type wordtype = array [1..WORDLEN] of char;    { Word string }
var words: array [1..NUMWORDS] of wordtype;    { List of five words }

FUNCTION ContainsVowel(s: wordtype): boolean;
var i: integer;
chk: boolean;
begin
i:=Length(s);
chk:=false;
while (i>0) and (not chk) do
if Upcase(s[i]) in ['A','E','I','O','U'] then
chk:=true
else
Dec(i);
ContainsVowel:=chk  { Set to true to test validity if desired }
end; {ContainsVowel}

var i,j: integer;
BEGIN
Randomize;  { Comment out while testing }
for i:=1 to NUMWORDS do
begin
repeat
for j:=1 to WORDLEN do
words[i][j]:=Chr(Random(25)+65);    { Build word }
until ContainsVowel(words[i]);              { Check word }
end;
writeln('Generated ',NUMWORDS,' words: ');
for i:=1 to NUMWORDS do write(words[i]:10);
writeln
END.

As you can see, the main program simply consists of  a repeat loop that
continues to build words until it gets one that contains a vowel.  When
it does, the outer for loop advances to the next word.

--
Robert B. Clark <rcl...@iquest.net>
"Always listen to experts.  They'll tell you what can't be done, and why.  Then do it." --RAH