Advertisement
simonradev

1. Largest Common End

Oct 14th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. namespace ConsoleApp1
  2. {
  3.     using System;
  4.  
  5.     public class StartUp
  6.     {
  7.         public static void Main()
  8.         {
  9.             char[] splitSymbols = new[] { ' ' };
  10.             string[] firstWords = SplitString(Console.ReadLine(), splitSymbols);
  11.             string[] secondWords = SplitString(Console.ReadLine(), splitSymbols);
  12.  
  13.             int leftCompare = CompareStringArrays(firstWords, secondWords);
  14.  
  15.             Reverse(firstWords);
  16.             Reverse(secondWords);
  17.             int rightCompare = CompareStringArrays(firstWords, secondWords);
  18.  
  19.             Console.WriteLine(Math.Max(leftCompare, rightCompare));
  20.         }
  21.  
  22.         public static void Reverse(string[] arrayToReverse)
  23.         {
  24.             for (int currentIndex = 0; currentIndex < arrayToReverse.Length / 2; currentIndex++)
  25.             {
  26.                 int lastIndex = arrayToReverse.Length - 1 - currentIndex;
  27.                 string holder = arrayToReverse[lastIndex];
  28.                 arrayToReverse[lastIndex] = arrayToReverse[currentIndex];
  29.                 arrayToReverse[currentIndex] = holder;
  30.             }
  31.         }
  32.  
  33.         public static int CompareStringArrays(string[] firstArray, string[] secondArray)
  34.         {
  35.             int counter = 0;
  36.  
  37.             int smallerLength = Math.Min(firstArray.Length, secondArray.Length);
  38.             for (int currentElement = 0; currentElement < smallerLength; currentElement++)
  39.             {
  40.                 string firstElement = firstArray[currentElement];
  41.                 string secondElement = secondArray[currentElement];
  42.  
  43.                 if (firstElement.Equals(secondElement))
  44.                 {
  45.                     counter++;
  46.                 }
  47.                 else
  48.                 {
  49.                     break;
  50.                 }
  51.             }
  52.  
  53.             return counter;
  54.         }
  55.  
  56.         public static string[] SplitString(string stringToSplit, char[] splitSymbols)
  57.         {
  58.             return stringToSplit.Split(splitSymbols, StringSplitOptions.RemoveEmptyEntries);
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement