-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (42 loc) · 1.69 KB
/
Program.cs
File metadata and controls
56 lines (42 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Задача: Написать программу, которая из имеющегося массива строк формирует новый массив из строк, длина которых
//меньше, либо равна 3 символам. Первоначальный массив можно ввести с клавиатуры, либо задать на старте выполнения
// алгоритма. При решении не рекомендуется пользоваться коллекциями, лучше обойтись исключительно массивами.
// Примеры:
// [“Hello”, “2”, “world”, “:-)”] → [“2”, “:-)”]
// [“1234”, “1567”, “-2”, “computer science”] → [“-2”]
// [“Russia”, “Denmark”, “Kazan”] → []
using System; using static System.Console;
Clear();
string[] array = AskArray();
string[] result = FindLessThan(array, 3);
System.Console.WriteLine($"[{string.Join(", ", array)}] -> [{string.Join(", ", result)}]");
string[] FindLessThan(string[] input, int n)
{
string[] output = new string[CountLessThan(input, n)];
for (int i = 0, j = 0; i < input.Length; i++)
{
if (input[i].Length <= n)
{
output[j] = input[i];
j++;
}
}
return output;
}
int CountLessThan(string[] input, int n)
{
int count = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i].Length <= n)
{
count++;
}
}
return count;
}
string[] AskArray()
{
System.Console.Write("Введите значения через пробел: ");
return ReadLine().Split(" ");
}