
C клавиатуры вводится произвольная строка символов. Группы символов, разделенные пробелами и не
имеющие пробелов внутри, будем называть словами. Написать программу, которая удаляет все слова с нечетными номерами. Вывести полученную строку на экран. Длина строки не более 100 символов.

Ответы на вопрос

// PascalABC.NET 3.1, сборка 1256 от 21.06.2016
begin
var s:=ReadlnString('>');
var m:=s.Matches('(\S+)').Select(x->(x.Index+1,x.Value.Length));
// получили последовательность элементов вида <позиция><длина>,
// отражающих положение слов в строке.
var n:=m.Count; // количество слов
if n.IsEven then Dec(n);
while n>0 do begin
var e:=m.ElementAt(n-1);
Delete(s,e.Item1,e.Item2);
Dec(n,2)
end;
Writeln(s)
end.
Тестовое решение
> Карл у Клары украл кораллы Клара у Карла украла кларнет
у украл Клара Карла кларнет



//Pascal ABC.NET 3.1 сборка 1219
Var
s:string;
i:integer;
begin
readln(s);
s:=s.Trim;
i:=1;
while i<=length(s)-1 do
if (s[i]=' ') and (s[i+1]=' ') then delete(s,i,1) else inc(i);
var ars:=s.Split;
s:='';
i:=1;
while i<=ars.length-1 do
begin
s:=s+ars[i]+' ';
i:=i+2;
end;
writeln(s);
end.
Пример ввода:
С понедельника точно начну писать документацию. Вот пока немного годноты.
Пример вывода:
понедельника начну документацию. пока годноты.



Problem Statement
You are given a string of characters inputted from the keyboard. Groups of characters separated by spaces and without spaces within them are considered words. Your task is to write a program that removes all words with odd indices and outputs the resulting string. The length of the string will not exceed 100 characters.
Solution
To solve this problem, we can follow these steps:
1. Read the input string from the keyboard. 2. Split the string into a list of words using the space character as the delimiter. 3. Remove words with odd indices from the list. 4. Join the remaining words back into a string using the space character as the separator. 5. Print the resulting string.
Here's the Python code that implements this solution:
```python # Step 1: Read the input string input_string = input("Enter a string: ")
# Step 2: Split the string into a list of words words = input_string.split()
# Step 3: Remove words with odd indices filtered_words = [word for index, word in enumerate(words) if index % 2 == 0]
# Step 4: Join the remaining words back into a string result_string = ' '.join(filtered_words)
# Step 5: Print the resulting string print(result_string) ```
Now, let's run an example to see how the program works:
Example:
Input: ``` Enter a string: Hello world! This is a test. ```
Output: ``` Hello This a ```
In this example, the input string "Hello world! This is a test." is split into a list of words: ["Hello", "world!", "This", "is", "a", "test."]. The words with odd indices ("world!" and "is") are removed, and the remaining words ("Hello", "This", "a") are joined back into a string, resulting in "Hello This a".
Please note that the program assumes that words are separated by spaces and does not consider other punctuation marks as word separators.
I hope this helps! Let me know if you have any further questions.


Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili