(* consonantvowel.pas - Determine how many strings of letters * contain a desired number of consonants and vowels. * But this time, insist that all the letters be different. * For example, for 2 consonants and 1 vowel --> 6300 strings * 3 consonants and 2 vowels --> 1,596,000 strings *) program consonantvowel2; const desiredConsonants = 3; desiredVowels = 2; var firstString, lastString, s : string; numVowels, i : integer; count : int32; (*******************************************************************) function isVowel(c : char) : boolean; var result : boolean; begin if (c = 'a') or (c = 'e') or (c = 'i') or (c = 'o') or (c = 'u') then result := true else result := false; isVowel := result; end; (*******************************************************************) function countVowels(s : string) : integer; var i, count : integer; begin count := 0; for i := 1 to length(s) do if isVowel(s[i]) then count := count + 1; countVowels := count; end; (*******************************************************************) function nextString(s : string) : string; var i : integer; done : boolean; begin i := desiredConsonants + desiredVowels; done := false; while not done do begin (* As long as the letter is not z, go to next letter. *) if s[i] <> 'z' then begin s[i] := chr(ord(s[i]) + 1); done := true; end (* In case of z, back up a letter and try incrementing it. *) else s[i] := 'a'; i := i - 1; if i = 0 then done := true; end; nextString := s; end; (*******************************************************************) function anyDuplication(s : string) : boolean; var i, j : integer; foundDuplicate : boolean; begin foundDuplicate := false; for i := 1 to length(s) - 1 do for j := i+1 to length(s) do if s[i] = s[j] then foundDuplicate := true; anyDuplication := foundDuplicate; end; (*******************************************************************) (* MAIN PROGRM *) begin (* Start the string at 'aaaaa'. Cycle through each possible string one at * a time like an odometer, until we reach 'zzzzz'. *) firstString := ''; lastString := ''; for i := 1 to desiredConsonants + desiredVowels do begin firstString := firstString + 'a'; lastString := lastString + 'z'; end; s := firstString; count := 0; repeat numVowels := countVowels(s); if (numVowels = desiredVowels) and (anyDuplication(s) = false) then begin writeln(s); count := count + 1; end; s := nextString(s); until s = lastString; writeln('I found ', count, ' strings with ', desiredConsonants, ' consonants and ', desiredVowels, ' vowels.'); end.