Advertisement
Guest User

дз строки в консоли

a guest
Mar 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace дз_строки_в_консоли
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string String;
  14.             string text;
  15.             char Char;
  16.             int count = 0;
  17.             int count2 = 0;
  18.  
  19.             int count3 = 0;
  20.             int countMax;
  21.  
  22.             bool inWord;
  23.             bool inWord2;
  24.             int index;
  25.  
  26.             string newString = "";
  27.  
  28.            
  29.  
  30.             // Zadacha 1
  31.             Console.WriteLine("Задача 1");
  32.             Console.WriteLine("Введите строку: ");
  33.             String = Console.ReadLine();
  34.  
  35.             foreach (char Char1 in String)
  36.             {
  37.                 if (char.IsDigit(Char1))
  38.                 {
  39.                     count += 1;
  40.                 }
  41.             }
  42.  
  43.             Console.WriteLine("Количество цифр: " + count);
  44.             Console.WriteLine();
  45.  
  46.             // Задача 2
  47.             Console.WriteLine();
  48.             Console.WriteLine("Задача 2");
  49.             Console.WriteLine("Введите строку: ");
  50.             // Количество слогов равно количеству гласных
  51.             string stringGlasn = "аеёиоуыэюяaeiouy"; // гласные
  52.             String = Console.ReadLine();
  53.             count = 0;
  54.  
  55.             foreach (char letter in String)
  56.             {
  57.                 foreach (char letterGlasn in stringGlasn)
  58.                 {
  59.                     if (letter == letterGlasn)
  60.                     {
  61.                         count += 1;
  62.                     }
  63.                 }
  64.             }
  65.             Console.WriteLine("Количество слогов в строке: " + count);
  66.             Console.WriteLine();
  67.  
  68.             // Zadacha 3
  69.             Console.WriteLine();
  70.             Console.WriteLine("Задача 3");
  71.             Console.WriteLine("Введите символ");
  72.             Char = Console.ReadLine()[0];
  73.  
  74.             Console.WriteLine("Введите строку: ");
  75.             String = Console.ReadLine();
  76.  
  77.             count = 0;
  78.  
  79.             foreach (Char Char1 in String)
  80.             {
  81.                 if (Char1 == Char)
  82.                 {
  83.                     count += 1;
  84.                 }
  85.             }
  86.  
  87.             Console.WriteLine("Количество вхождений символа в строку: " + count);
  88.             Console.WriteLine();
  89.  
  90.             // Zadacha 4
  91.             Console.WriteLine();
  92.             Console.WriteLine("Задача 4");
  93.             Console.WriteLine("Введите строку: ");
  94.             text = Console.ReadLine();
  95.             inWord = false;
  96.             count = 0;
  97.             count2 = 0;
  98.  
  99.             for (int i = 0; i < text.Length; i++)
  100.             {
  101.                 if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой      
  102.                 {
  103.                     count2 += 1;
  104.                     if (inWord == false)
  105.                     {
  106.                         inWord = true;
  107.                     }
  108.                 }
  109.                 else
  110.                 {
  111.                     inWord = false;
  112.                     if (count2 < 5)
  113.                     {
  114.                         count += 1;
  115.                     }
  116.                     count2 = 0;
  117.                 }
  118.             }
  119.             if (count2 < 5) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
  120.             {
  121.                 count += 1;
  122.             }
  123.  
  124.             Console.WriteLine("Количество слов из меньше чем 5 букв: " + count);
  125.  
  126.             // Zadacha 5
  127.             Console.WriteLine();
  128.             Console.WriteLine("Задача 5");
  129.             Console.WriteLine("Введите строку: ");
  130.             text = Console.ReadLine();
  131.             inWord = false;
  132.             count = 0;
  133.             count2 = 0;
  134.  
  135.             for (int i = 0; i < text.Length; i++)
  136.             {
  137.                 if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой      
  138.                 {
  139.                     count2 += 1;
  140.                     if (inWord == false)
  141.                     {
  142.                         inWord = true;
  143.                     }
  144.                 }
  145.                 else
  146.                 {
  147.                     inWord = false;
  148.                     if (count2 == 1)
  149.                     {
  150.                         count += 1;
  151.                     }
  152.                     count2 = 0;
  153.                 }
  154.             }
  155.             if (count2 == 1) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
  156.             {
  157.                 count += 1;
  158.             }
  159.  
  160.             Console.WriteLine("Количество слов из одной буквы: " + count);
  161.  
  162.  
  163.             // Задача 6
  164.             Console.WriteLine();
  165.             Console.WriteLine("Задача 6");
  166.             Console.WriteLine("Введите строку: ");
  167.             text = Console.ReadLine();
  168.             inWord = false;
  169.             count = 0;
  170.             count2 = 0;
  171.  
  172.             for (int i = 0; i < text.Length; i++)
  173.             {
  174.                 if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой      
  175.                 {
  176.                     count2 += 1;
  177.                     if (inWord == false)
  178.                     {
  179.                         inWord = true;
  180.                     }
  181.                 }
  182.                 else
  183.                 {
  184.                     inWord = false;
  185.                     if (count2 == 1)
  186.                     {
  187.                         index = i - 2;
  188.                         text = text.Remove(index, 1);
  189.                         count += 1;
  190.                     }
  191.                     count2 = 0;
  192.                 }
  193.             }
  194.             if (count2 == 1) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
  195.             {
  196.                 index = text.Length - 1 - 2;
  197.                 text = text.Remove(index, 1);
  198.                 count += 1;
  199.             }
  200.  
  201.             Console.WriteLine("Строка без слов состоящих из 1 буквы: " + text);
  202.  
  203.             // Задача 7
  204.             Console.WriteLine();
  205.             Console.WriteLine("Задача 7");
  206.             Console.WriteLine("Введите строку: ");
  207.             text = Console.ReadLine();
  208.             inWord = false;
  209.             count = 0;
  210.             count2 = 0;
  211.  
  212.             for (int i = 0; i < text.Length; i++)
  213.             {
  214.                 if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой      
  215.                 {
  216.                     count2 += 1;
  217.                     if (inWord == false)
  218.                     {
  219.                         inWord = true;
  220.                     }
  221.                 }
  222.                 else
  223.                 {
  224.                     inWord = false;
  225.                     if (count2 > 6)
  226.                     {
  227.                         index = i - (count2 + 1);
  228.                         text = text.Remove(index, count2);
  229.                         count += 1;
  230.                     }
  231.                     count2 = 0;
  232.                 }
  233.             }
  234.             if (count2 > 6) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
  235.             {
  236.                 index = text.Length - 1 - (count2 + 1);
  237.                 text = text.Remove(index, count2);
  238.                 count += 1;
  239.             }
  240.  
  241.             Console.WriteLine("Строка без слов состоящих из больше чем 6 букв: " + text);
  242.  
  243.            
  244.  
  245.             // Задача 8
  246.             Console.WriteLine();
  247.             Console.WriteLine("Задача 8");
  248.             Console.WriteLine("Сгруппировать слова одинаковой длины: ");
  249.             text = Console.ReadLine();
  250.             inWord = false;
  251.             inWord2 = false;
  252.             count2 = 0;
  253.             count3 = 0;
  254.             newString = "";
  255.             string subWord1 = "";
  256.             string subWord2 = "";
  257.  
  258.             for (int i = 0; i < text.Length; i++)
  259.             {
  260.                 if (text[i] != ' ' && char.IsLetter(text[i])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой      
  261.                 {
  262.                     count2 += 1;
  263.                     if (inWord == false)
  264.                     {
  265.                         inWord = true;
  266.  
  267.                         newString += " ";
  268.                     }
  269.                     subWord1 += text[i];
  270.                 }
  271.                 else
  272.                 {
  273.                     inWord = false;
  274.  
  275.                     count3 = 0;
  276.  
  277.                     for (int j = i + 1; j < text.Length; j++)
  278.                     {
  279.                         if (text[j] != ' ' && char.IsLetter(text[j])) // char.IsLetter() - проверка, что символ является буквой. char.IsDigit() - что цифрой      
  280.                         {
  281.                             count3 += 1;
  282.                             subWord2 += text[j];
  283.                             if (inWord2 == false)
  284.                             {
  285.                                 inWord2 = true;
  286.                             }
  287.                         }
  288.                         else
  289.                         {
  290.                             inWord2 = false;
  291.                             if(count2 == count3)
  292.                             {
  293.                                 newString += subWord1 + " " + subWord2;
  294.                             }
  295.                             count3 = 0;
  296.                             subWord2 = "";
  297.                         }
  298.                     }
  299.                     if (count3 == count2) // Потому что если после последнего слова не стоит пробел, выход из слова не происходит. Поэтому нужна еще одна проверка было ли мненьше 5 символов в слове
  300.                     {
  301.  
  302.                         newString += subWord1 + " " + subWord2;
  303.                     }
  304.  
  305.                     count2 = 0;
  306.                     subWord1 = "";
  307.                 }
  308.             }
  309.  
  310.             Console.WriteLine(newString);
  311.             Console.ReadLine();
  312.  
  313.             // Задача 9
  314.             Console.WriteLine();
  315.             Console.WriteLine("Задача 9");
  316.             Console.WriteLine("Введите строку (количество цифр подряд): ");
  317.             text = Console.ReadLine();
  318.             count = 0;
  319.             countMax = 0;
  320.  
  321.             foreach (char letter in text)
  322.             {
  323.                 if (char.IsDigit(letter))
  324.                 {
  325.                     count += 1;
  326.                     if(count > countMax)
  327.                     {
  328.                         countMax = count;
  329.                     }
  330.                 }
  331.                 else
  332.                 {
  333.                     count = 0;
  334.                 }
  335.             }
  336.  
  337.             Console.WriteLine("Наибольшее количество цифр подряд: " + countMax);
  338.  
  339.             // Задача 10
  340.             Console.WriteLine();
  341.             Console.WriteLine("Задача 10");
  342.             Console.WriteLine("Введите строку (заменить * на -): ");
  343.             text = Console.ReadLine();
  344.             newString = "";
  345.  
  346.             foreach (char letter in text)
  347.             {
  348.                 if (letter == '*')
  349.                 {
  350.                     newString += "-";
  351.                 }
  352.                 else
  353.                 {
  354.                     newString += letter;
  355.                 }
  356.             }
  357.  
  358.             Console.WriteLine("Результат: " + newString);
  359.  
  360.             // Задача 11
  361.             Console.WriteLine();
  362.             Console.WriteLine("Задача 11");
  363.             Console.WriteLine("Введите строку (заменить . на ...): ");
  364.             text = Console.ReadLine();
  365.             newString = "";
  366.  
  367.             foreach (char letter in text)
  368.             {
  369.                 if (letter == '.')
  370.                 {
  371.                     newString += "...";
  372.                 }
  373.                 else
  374.                 {
  375.                     newString += letter;
  376.                 }
  377.             }
  378.  
  379.             Console.WriteLine("Результат: " + newString);
  380.             Console.ReadLine();
  381.         }
  382.     }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement