Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace дз_строки_в_консоли
- {
- class Program
- {
- static void Main(string[] args)
- {
- string String;
- string text;
- char Char;
- int count = 0;
- int count2 = 0;
- int count3 = 0;
- int countMax;
- bool inWord;
- bool inWord2;
- int index;
- string newString = "";
- // Zadacha 1
- Console.WriteLine("Задача 1");
- Console.WriteLine("Введите строку: ");
- String = Console.ReadLine();
- foreach (char Char1 in String)
- {
- if (char.IsDigit(Char1))
- {
- count += 1;
- }
- }
- Console.WriteLine("Количество цифр: " + count);
- Console.WriteLine();
- // Задача 2
- Console.WriteLine();
- Console.WriteLine("Задача 2");
- Console.WriteLine("Введите строку: ");
- // Количество слогов равно количеству гласных
- string stringGlasn = "аеёиоуыэюяaeiouy"; // гласные
- String = Console.ReadLine();
- count = 0;
- foreach (char letter in String)
- {
- foreach (char letterGlasn in stringGlasn)
- {
- if (letter == letterGlasn)
- {
- count += 1;
- }
- }
- }
- Console.WriteLine("Количество слогов в строке: " + count);
- Console.WriteLine();
- // Zadacha 3
- Console.WriteLine();
- Console.WriteLine("Задача 3");
- Console.WriteLine("Введите символ");
- Char = Console.ReadLine()[0];
- Console.WriteLine("Введите строку: ");
- String = Console.ReadLine();
- count = 0;
- foreach (Char Char1 in String)
- {
- if (Char1 == Char)
- {
- count += 1;
- }
- }
- Console.WriteLine("Количество вхождений символа в строку: " + count);
- Console.WriteLine();
- // Zadacha 4
- Console.WriteLine();
- Console.WriteLine("Задача 4");
- Console.WriteLine("Введите строку: ");
- text = Console.ReadLine();
- inWord = false;
- count = 0;
- count2 = 0;
- for (int i = 0; i < text.Length; i++)
- {
- if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой
- {
- count2 += 1;
- if (inWord == false)
- {
- inWord = true;
- }
- }
- else
- {
- inWord = false;
- if (count2 < 5)
- {
- count += 1;
- }
- count2 = 0;
- }
- }
- if (count2 < 5) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
- {
- count += 1;
- }
- Console.WriteLine("Количество слов из меньше чем 5 букв: " + count);
- // Zadacha 5
- Console.WriteLine();
- Console.WriteLine("Задача 5");
- Console.WriteLine("Введите строку: ");
- text = Console.ReadLine();
- inWord = false;
- count = 0;
- count2 = 0;
- for (int i = 0; i < text.Length; i++)
- {
- if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой
- {
- count2 += 1;
- if (inWord == false)
- {
- inWord = true;
- }
- }
- else
- {
- inWord = false;
- if (count2 == 1)
- {
- count += 1;
- }
- count2 = 0;
- }
- }
- if (count2 == 1) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
- {
- count += 1;
- }
- Console.WriteLine("Количество слов из одной буквы: " + count);
- // Задача 6
- Console.WriteLine();
- Console.WriteLine("Задача 6");
- Console.WriteLine("Введите строку: ");
- text = Console.ReadLine();
- inWord = false;
- count = 0;
- count2 = 0;
- for (int i = 0; i < text.Length; i++)
- {
- if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой
- {
- count2 += 1;
- if (inWord == false)
- {
- inWord = true;
- }
- }
- else
- {
- inWord = false;
- if (count2 == 1)
- {
- index = i - 2;
- text = text.Remove(index, 1);
- count += 1;
- }
- count2 = 0;
- }
- }
- if (count2 == 1) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
- {
- index = text.Length - 1 - 2;
- text = text.Remove(index, 1);
- count += 1;
- }
- Console.WriteLine("Строка без слов состоящих из 1 буквы: " + text);
- // Задача 7
- Console.WriteLine();
- Console.WriteLine("Задача 7");
- Console.WriteLine("Введите строку: ");
- text = Console.ReadLine();
- inWord = false;
- count = 0;
- count2 = 0;
- for (int i = 0; i < text.Length; i++)
- {
- if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой
- {
- count2 += 1;
- if (inWord == false)
- {
- inWord = true;
- }
- }
- else
- {
- inWord = false;
- if (count2 > 6)
- {
- index = i - (count2 + 1);
- text = text.Remove(index, count2);
- count += 1;
- }
- count2 = 0;
- }
- }
- if (count2 > 6) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
- {
- index = text.Length - 1 - (count2 + 1);
- text = text.Remove(index, count2);
- count += 1;
- }
- Console.WriteLine("Строка без слов состоящих из больше чем 6 букв: " + text);
- // Задача 8
- Console.WriteLine();
- Console.WriteLine("Задача 8");
- Console.WriteLine("Сгруппировать слова одинаковой длины: ");
- text = Console.ReadLine();
- inWord = false;
- inWord2 = false;
- count2 = 0;
- count3 = 0;
- newString = "";
- string subWord1 = "";
- string subWord2 = "";
- for (int i = 0; i < text.Length; i++)
- {
- if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой
- {
- count2 += 1;
- if (inWord == false)
- {
- inWord = true;
- newString += " ";
- }
- subWord1 += text[i];
- }
- else
- {
- inWord = false;
- count3 = 0;
- for (int j = i + 1; j < text.Length; j++)
- {
- if (text[j] != ' ' && char.IsLetter(text[j])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой
- {
- count3 += 1;
- subWord2 += text[j];
- if (inWord2 == false)
- {
- inWord2 = true;
- }
- }
- else
- {
- inWord2 = false;
- if(count2 == count3)
- {
- newString += subWord1 + " " + subWord2;
- }
- count3 = 0;
- subWord2 = "";
- }
- }
- if (count3 == count2) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
- {
- newString += subWord1 + " " + subWord2;
- }
- count2 = 0;
- subWord1 = "";
- }
- }
- Console.WriteLine(newString);
- Console.ReadLine();
- // Задача 9
- Console.WriteLine();
- Console.WriteLine("Задача 9");
- Console.WriteLine("Введите строку (количество цифр подряд): ");
- text = Console.ReadLine();
- count = 0;
- countMax = 0;
- foreach (char letter in text)
- {
- if (char.IsDigit(letter))
- {
- count += 1;
- if(count > countMax)
- {
- countMax = count;
- }
- }
- else
- {
- count = 0;
- }
- }
- Console.WriteLine("Наибольшее количество цифр подряд: " + countMax);
- // Задача 10
- Console.WriteLine();
- Console.WriteLine("Задача 10");
- Console.WriteLine("Введите строку (заменить * на -): ");
- text = Console.ReadLine();
- newString = "";
- foreach (char letter in text)
- {
- if (letter == '*')
- {
- newString += "-";
- }
- else
- {
- newString += letter;
- }
- }
- Console.WriteLine("Результат: " + newString);
- // Задача 11
- Console.WriteLine();
- Console.WriteLine("Задача 11");
- Console.WriteLine("Введите строку (заменить . на ...): ");
- text = Console.ReadLine();
- newString = "";
- foreach (char letter in text)
- {
- if (letter == '.')
- {
- newString += "...";
- }
- else
- {
- newString += letter;
- }
- }
- Console.WriteLine("Результат: " + newString);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement