вывести на экран в алфавитном порядке все слова строки, в которых есть четыре подряд идущие согласные буквы
вот мой код. я не знаю, как обратиться к элементам строкового массива. в первой строке выдает ошибку "ожидался тип".
var a: array ['б', 'в', 'г', 'д', 'ж', 'з', 'й', 'к', 'л', 'м', 'н', 'п', 'р', 'с', 'т', 'ф', 'х', 'ц', 'ч', 'ш', 'щ'] of string;
s:string;
i:byte;
begin
write('строка: ');
readln(s);
for i:=1 to length(s) do
if mas[i] in mas then
if mas[i+1] in mas then
if mas[i+2] in mas then
if mas[i+3] in mas then
writeln(mas[i]);
end.
Ответы (3 шт):
Автор решения: MBo
→ Ссылка
var a: array of string = ...
Как это точно записывается в вашем абэце, и можно ли там в var инициализировать динамический массив, я не знаю .
А то, что вы пытались написать, может при доработке быть интерпретировано как массив строк, индексированный символами из множества. Нужно это или нет - по коду, в котором a не используется - непонятно, но очень сомнительно.
Автор решения: Old Skull
→ Ссылка
Все предложенные мной в комментариях способы работают. Причём, меняется только объявление набора согласных.
program test;
uses
System.Text.RegularExpressions;
function CheckWordByArray(const s: string): Boolean;
const
consonants: array[00..20] of char =
('б', 'в', 'г', 'д', 'ж', 'з', 'й', 'к', 'л', 'м', 'н', 'п', 'р', 'с', 'т', 'ф', 'х', 'ц', 'ч', 'ш', 'щ');
begin
Result := s.length > 3;
if ( not Result ) then Exit();
for var i := 1 to length(s) - 3 do begin
Result := ( consonants.contains( s[i+0] ) )
and ( consonants.contains( s[i+1] ) )
and ( consonants.contains( s[i+2] ) )
and ( consonants.contains( s[i+3] ) );
{}
{
Result := ( s[i+0] in consonants )
and ( s[i+1] in consonants )
and ( s[i+2] in consonants )
and ( s[i+3] in consonants );
{}
if ( Result ) then Exit();
end;
end;
function CheckWordByString(const s: string): Boolean;
const
consonants = 'бвгджзйклмнпрстфхцчшщ';
begin
Result := s.length > 3;
if ( not Result ) then Exit();
for var i := 1 to length(s) - 3 do begin
{
Result := ( consonants.contains( s[i+0] ) )
and ( consonants.contains( s[i+1] ) )
and ( consonants.contains( s[i+2] ) )
and ( consonants.contains( s[i+3] ) );
{}
Result := ( s[i+0] in consonants )
and ( s[i+1] in consonants )
and ( s[i+2] in consonants )
and ( s[i+3] in consonants );
{}
if ( Result ) then Exit();
end;
end;
function CheckWordBySet(const s: string): Boolean;
const
consonants = ['б', 'в', 'г', 'д', 'ж', 'з', 'й', 'к', 'л', 'м', 'н', 'п', 'р', 'с', 'т', 'ф', 'х', 'ц', 'ч', 'ш', 'щ'];
begin
Result := s.length > 3;
if ( not Result ) then Exit();
for var i := 1 to length(s) - 3 do begin
{
Result := ( consonants.contains( s[i+0] ) )
and ( consonants.contains( s[i+1] ) )
and ( consonants.contains( s[i+2] ) )
and ( consonants.contains( s[i+3] ) );
{}
Result := ( s[i+0] in consonants )
and ( s[i+1] in consonants )
and ( s[i+2] in consonants )
and ( s[i+3] in consonants );
{}
if ( Result ) then Exit();
end;
end;
function CheckWordByRegEx(const s: string): Boolean;
begin
Result := Regex.IsMatch(s, '[бвгджзйклмнпрстфхцчшщ]{4}');
// Result := Regex.IsMatch(s, '[^аеёиоуыэюя]{4}');
end;
function CheckWordOther(const s: string): Boolean;
const
vowels = 'аеёиоуыэюя';
begin
var counter: Byte := 0;
foreach var c: char in s do begin
if ( vowels.contains(c) ) then
counter := 0
else
Inc(counter);
if ( counter > 3 ) then Break;
end;
Result := counter > 3;
end;
procedure DoTest(const s: string; const expected: Boolean);
begin
Assert(CheckWordByArray(s) = expected);
Assert(CheckWordByString(s) = expected);
Assert(CheckWordBySet(s) = expected);
Assert(CheckWordByRegEx(s) = expected);
Assert(CheckWordOther(s) = expected);
end;
begin
DoTest('строка', false);
DoTest('контрмера', true );
DoTest('к', false);
WriteLn('Press Enter.');
ReadLn();
end.
Автор решения: RAlex
→ Ссылка
## // PasscalАВС.NЕТ 3.8.3
var Согласные := 'бвгджзйклмнпрстфхцчшщ';
var a := ReadlnString('строка: ').ToWords;
var regexp := '.*[' + Согласные + ']{4,}.*';
a.Where(w -> w.IsMatch(regexp)).Print