NyanCoder

lab2_functions

Dec 24th, 2020 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using static System.String;
  4.  
  5. namespace _2020_12_24_lab2
  6. {
  7.     class Program
  8.     {
  9.         static void Copies(string s, ref string s1, int n)
  10.         {
  11.             for (int i = 0; i < n; i++)
  12.                 Concat(ref s1, s);
  13.         }
  14.         static int Words(string s)
  15.         {
  16.             int i = 0;
  17.             bool isLetter = false;
  18.             foreach (char ch in s)
  19.             {
  20.                 bool currentIsLetter = Char.IsLetter(ch);
  21.                 if (currentIsLetter && !isLetter)
  22.                     i++;
  23.                 isLetter = currentIsLetter;
  24.             }
  25.             return i;
  26.         }
  27.         static void Concat(ref string s1, string s2)
  28.         {
  29.             s1 += s2;
  30.         }
  31.         static string[] Parse(string s, char t)
  32.         {
  33.             string[] result = { "", "" };
  34.             bool find = false;
  35.             foreach (char ch in s)
  36.             {
  37.                 if (ch == t)
  38.                     find = true;
  39.                 result[find ? 1 : 0] += ch;
  40.             }
  41.             return result;
  42.         }
  43.         static void Center(string s1, ref string s2, int l)
  44.         {
  45.             // if l is odd then the middle character replaced with s1
  46.             // if l more than length of s2 then add a space to fit the lenght
  47.             if (l < 2)
  48.                 s2 = s1;
  49.             else
  50.             {
  51.                 int secondHalfStart = l / 2 + l % 2;
  52.                 if (secondHalfStart < s2.Length)
  53.                 {
  54.                     string trail = s2.Substring(secondHalfStart) + new String(' ', l / 2 - (s2.Length - secondHalfStart)); // fill second half with space characters
  55.                     s2 = s2.Substring(0, l / 2) + s1 + trail;
  56.                 }
  57.                 else
  58.                 {
  59.                     s2 += new String(' ', l / 2 - s2.Length) + s1 + new String(' ', l / 2); // fill first half and add s1 and add fake secondhalf
  60.                 }
  61.             }
  62.         }
  63.         static void Delete(ref string s, int n, int l)
  64.         {
  65.             string trail = (s.Length > n + l) ? s.Substring(n + l) : "";
  66.             s = ((n < s.Length) ? s.Substring(0, n) : s) + trail;
  67.         }
  68.         static void Left(ref string s, int l)
  69.         {
  70.             if (l <= s.Length)
  71.                 return;
  72.             s += new String(' ', l - s.Length);
  73.         }
  74.         static void Right(ref string s, int l)
  75.         {
  76.             if (l <= s.Length)
  77.                 return;
  78.             s = s.Insert(0, new String(' ', l - s.Length));
  79.         }
  80.         static string[] SubWord(string s, int n, int l)
  81.         {
  82.             List<string> result = new List<string>();
  83.  
  84.             if (l > 0)
  85.             {
  86.                 string temp = "";
  87.                 bool isLetter = false;
  88.                 int currentWordIndex = 0;
  89.                 foreach (char ch in s)
  90.                 {
  91.                     bool currentIsLetter = Char.IsLetter(ch);
  92.                     if (currentIsLetter && !isLetter)
  93.                     {
  94.                         currentWordIndex++;
  95.                         if (currentWordIndex > n + l)
  96.                             break;
  97.                     }
  98.                     if (currentWordIndex > n)
  99.                     {
  100.                         if (!currentIsLetter && isLetter)
  101.                         {
  102.                             result.Add(temp);
  103.                             temp = "";
  104.                         }
  105.                         if (currentIsLetter)
  106.                             temp += ch;
  107.                     }
  108.                     isLetter = currentIsLetter;
  109.                 }
  110.  
  111.                 if (temp.Length > 0)
  112.                     result.Add(temp);
  113.             }
  114.  
  115.             return result.ToArray();
  116.         }
  117.         static void Replace(ref string s, string s1, string s2)
  118.         {
  119.             string source = s;
  120.             Func<int, bool> checkForEquality = (int startPosition) => (!(source.Length - s1.Length - startPosition < 0) && (source.Substring(startPosition, s1.Length) == s1));
  121.             s = "";
  122.             for (int i = 0; i < source.Length;)
  123.             {
  124.                 if (checkForEquality(i))
  125.                 {
  126.                     s += s2;
  127.                     i += s1.Length;
  128.                 }
  129.                 else
  130.                 {
  131.                     s += source[i];
  132.                     i++;
  133.                 }
  134.             }
  135.         }
  136.         static void Main(string[] args)
  137.         {
  138.             string s1 = "something";
  139.             Copies("-blah", ref s1, 3);
  140.             Console.WriteLine(Format("Copies(\"-blah\", ref s1, 3): {0}", s1));
  141.  
  142.             Console.WriteLine(Format("Words(\"{0}\"): {1}", s1, Words(s1)));
  143.  
  144.             Concat(ref s1, " uhoh");
  145.             Console.WriteLine(Format("Concat(ref s1, \" uhoh\"): {0}", s1));
  146.  
  147.             {
  148.                 var strings = Parse(s1, '-');
  149.                 Console.WriteLine(Format("Parse(s1, '-'):\n\t{0}\n\t{1}", strings[0], strings[1]));
  150.             }
  151.  
  152.             Console.Write(Format("Center(\"ooga\", \"{0}\", {1}): ", s1, s1.Length));
  153.             Center("ooga", ref s1, s1.Length);
  154.             Console.WriteLine(s1);
  155.  
  156.             Console.Write(Format("Delete(\"{0}\", {1}, 2): ", s1, s1.Length - 3));
  157.             Delete(ref s1, s1.Length - 3, 2);
  158.             Console.WriteLine(s1);
  159.  
  160.             { // do not modify the s1
  161.                 string temp = s1;
  162.                 Left(ref temp, s1.Length + 2);
  163.                 Console.WriteLine(Format("Left(\"{0}\", {1}): {2}", s1, s1.Length + 2, temp));
  164.  
  165.                 temp = s1;
  166.                 Right(ref temp, s1.Length + 2);
  167.                 Console.WriteLine(Format("Right(\"{0}\", {1}): {2}", s1, s1.Length + 2, temp));
  168.             }
  169.  
  170.             {
  171.                 Console.WriteLine(Format("SubWord(\"{0}\", {1}, {2}):", s1, 1, 2));
  172.                 var strings = SubWord(s1, 1, 2);
  173.                 foreach (string str in strings)
  174.                     Console.WriteLine(Format("\t{0}", str));
  175.             }
  176.  
  177.             Console.Write(Format("Replace(\"{0}\", \"{1}\", \"{2}\"): ", s1, "blah", "ouch"));
  178.             Replace(ref s1, "blah", "ouch");
  179.             Console.WriteLine(s1);
  180.         }
  181.     }
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment