Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using static System.String;
- namespace _2020_12_24_lab2
- {
- class Program
- {
- static void Copies(string s, ref string s1, int n)
- {
- for (int i = 0; i < n; i++)
- Concat(ref s1, s);
- }
- static int Words(string s)
- {
- int i = 0;
- bool isLetter = false;
- foreach (char ch in s)
- {
- bool currentIsLetter = Char.IsLetter(ch);
- if (currentIsLetter && !isLetter)
- i++;
- isLetter = currentIsLetter;
- }
- return i;
- }
- static void Concat(ref string s1, string s2)
- {
- s1 += s2;
- }
- static string[] Parse(string s, char t)
- {
- string[] result = { "", "" };
- bool find = false;
- foreach (char ch in s)
- {
- if (ch == t)
- find = true;
- result[find ? 1 : 0] += ch;
- }
- return result;
- }
- static void Center(string s1, ref string s2, int l)
- {
- // if l is odd then the middle character replaced with s1
- // if l more than length of s2 then add a space to fit the lenght
- if (l < 2)
- s2 = s1;
- else
- {
- int secondHalfStart = l / 2 + l % 2;
- if (secondHalfStart < s2.Length)
- {
- string trail = s2.Substring(secondHalfStart) + new String(' ', l / 2 - (s2.Length - secondHalfStart)); // fill second half with space characters
- s2 = s2.Substring(0, l / 2) + s1 + trail;
- }
- else
- {
- s2 += new String(' ', l / 2 - s2.Length) + s1 + new String(' ', l / 2); // fill first half and add s1 and add fake secondhalf
- }
- }
- }
- static void Delete(ref string s, int n, int l)
- {
- string trail = (s.Length > n + l) ? s.Substring(n + l) : "";
- s = ((n < s.Length) ? s.Substring(0, n) : s) + trail;
- }
- static void Left(ref string s, int l)
- {
- if (l <= s.Length)
- return;
- s += new String(' ', l - s.Length);
- }
- static void Right(ref string s, int l)
- {
- if (l <= s.Length)
- return;
- s = s.Insert(0, new String(' ', l - s.Length));
- }
- static string[] SubWord(string s, int n, int l)
- {
- List<string> result = new List<string>();
- if (l > 0)
- {
- string temp = "";
- bool isLetter = false;
- int currentWordIndex = 0;
- foreach (char ch in s)
- {
- bool currentIsLetter = Char.IsLetter(ch);
- if (currentIsLetter && !isLetter)
- {
- currentWordIndex++;
- if (currentWordIndex > n + l)
- break;
- }
- if (currentWordIndex > n)
- {
- if (!currentIsLetter && isLetter)
- {
- result.Add(temp);
- temp = "";
- }
- if (currentIsLetter)
- temp += ch;
- }
- isLetter = currentIsLetter;
- }
- if (temp.Length > 0)
- result.Add(temp);
- }
- return result.ToArray();
- }
- static void Replace(ref string s, string s1, string s2)
- {
- string source = s;
- Func<int, bool> checkForEquality = (int startPosition) => (!(source.Length - s1.Length - startPosition < 0) && (source.Substring(startPosition, s1.Length) == s1));
- s = "";
- for (int i = 0; i < source.Length;)
- {
- if (checkForEquality(i))
- {
- s += s2;
- i += s1.Length;
- }
- else
- {
- s += source[i];
- i++;
- }
- }
- }
- static void Main(string[] args)
- {
- string s1 = "something";
- Copies("-blah", ref s1, 3);
- Console.WriteLine(Format("Copies(\"-blah\", ref s1, 3): {0}", s1));
- Console.WriteLine(Format("Words(\"{0}\"): {1}", s1, Words(s1)));
- Concat(ref s1, " uhoh");
- Console.WriteLine(Format("Concat(ref s1, \" uhoh\"): {0}", s1));
- {
- var strings = Parse(s1, '-');
- Console.WriteLine(Format("Parse(s1, '-'):\n\t{0}\n\t{1}", strings[0], strings[1]));
- }
- Console.Write(Format("Center(\"ooga\", \"{0}\", {1}): ", s1, s1.Length));
- Center("ooga", ref s1, s1.Length);
- Console.WriteLine(s1);
- Console.Write(Format("Delete(\"{0}\", {1}, 2): ", s1, s1.Length - 3));
- Delete(ref s1, s1.Length - 3, 2);
- Console.WriteLine(s1);
- { // do not modify the s1
- string temp = s1;
- Left(ref temp, s1.Length + 2);
- Console.WriteLine(Format("Left(\"{0}\", {1}): {2}", s1, s1.Length + 2, temp));
- temp = s1;
- Right(ref temp, s1.Length + 2);
- Console.WriteLine(Format("Right(\"{0}\", {1}): {2}", s1, s1.Length + 2, temp));
- }
- {
- Console.WriteLine(Format("SubWord(\"{0}\", {1}, {2}):", s1, 1, 2));
- var strings = SubWord(s1, 1, 2);
- foreach (string str in strings)
- Console.WriteLine(Format("\t{0}", str));
- }
- Console.Write(Format("Replace(\"{0}\", \"{1}\", \"{2}\"): ", s1, "blah", "ouch"));
- Replace(ref s1, "blah", "ouch");
- Console.WriteLine(s1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment