Advertisement
Cinder1986

Lab18-3-2

Mar 27th, 2023 (edited)
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using System.Text.RegularExpressions;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         const int maxLineLength = 200;
  8.         const string sentenceEnds = ".!?";
  9.         const int minSentences = 3;
  10.         const string punctuationSigns = ",:;-";
  11.  
  12.         static bool checkPunctuation(string line)
  13.         {
  14.             Regex punctuation = new Regex(@"^([A-ZА-Я]{1}\w*(,|:|;|-){0,1}(\s((\w+-\w+)+|\w+)(,|:|;|-){0,1})*((\w+-\w+)+|(\w+)){1}(\.\.\.|!|\?|\.){1}){3,}$");
  15.             return punctuation.IsMatch(line);
  16.         }
  17.  
  18.         /*static int calcLineSentences(string line)
  19.         {
  20.             string temp = string.Empty;
  21.             int counter = 0;
  22.             foreach (char symbol in line)
  23.             {
  24.                 if (sentenceEnds.Contains(symbol) && temp.Length > 0)
  25.                 {
  26.                     temp = string.Empty;
  27.                     counter++;
  28.                 }
  29.                 else
  30.                     temp += symbol;
  31.             }
  32.             return counter;
  33.         }
  34.  
  35.         static string[] getLineSentences(string line)
  36.         {
  37.             string[] sentences = new string[calcLineSentences(line)];
  38.             string temp = string.Empty;
  39.             int i = 0;
  40.             foreach(char symbol in line)
  41.             {
  42.                 if(sentenceEnds.Contains(symbol) && temp.Length > 0)
  43.                 {
  44.                     sentences[i++] = temp;
  45.                     temp = string.Empty;
  46.                 }
  47.                 else
  48.                     temp += symbol;
  49.             }
  50.             return sentences;
  51.         }
  52.  
  53.         static bool checkSentences(string[] sentences)
  54.         {
  55.             bool isCorrect = sentences.Length >= minSentences;
  56.             int i = 0;
  57.             while(isCorrect && i < sentences.Length)
  58.             {
  59.                 if (checkPunctuation(sentences[i]))
  60.                     i++;
  61.                 else
  62.                     isCorrect= false;
  63.             }
  64.             return isCorrect;
  65.         }
  66.  
  67.         static bool checkLine(string line)
  68.         {
  69.             bool isCorrect = line.Length >= maxLineLength;
  70.             return isCorrect & checkSentences(getLineSentences(line));
  71.         }*/
  72.  
  73.         static string excludeEachNSymbol(string line, int N)
  74.         {
  75.             string cutLine = string.Empty;
  76.             for (int i = 0; i < line.Length; i++)
  77.                 if ((i + 1) % N != 0)
  78.                     cutLine += line[i];
  79.             return cutLine;
  80.         }
  81.  
  82.         static void Main(string[] args)
  83.         {
  84.             Console.Write("Enter a line: ");
  85.             string line = Console.ReadLine();
  86.             if (line.Length <= maxLineLength && checkPunctuation(line))
  87.             {
  88.                 if ((line.Length & 1) == 0)
  89.                     Console.Write(excludeEachNSymbol(line, 2));
  90.                 else
  91.                     Console.Write("The string is of odd length.");
  92.             }
  93.             else
  94.                 Console.Write("The string is not correct!");
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement