simeonnikolov

Untitled

Mar 10th, 2022 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     public static void Main(string[] args)
  8.     {
  9. //      string longestStr = "123gt323";
  10. //      string shortestStr = "23";
  11.        
  12.         string longestStr = "234d56";
  13.         string shortestStr = "345";
  14.        
  15.         if (longestStr.Length < shortestStr.Length)
  16.         {
  17.             string temp;
  18.             temp = longestStr;
  19.             longestStr = shortestStr;
  20.             shortestStr = temp;
  21.         }
  22.        
  23.         Console.WriteLine("Longest " + longestStr);
  24.         Console.WriteLine("Shortest " + shortestStr);
  25.        
  26.         if (longestStr.Contains(shortestStr))
  27.         {
  28.             int counter = 0;
  29.             int index = longestStr.IndexOf(shortestStr);
  30.             while (index != -1)
  31.             {
  32.                 counter++;
  33.                 index = longestStr.IndexOf(shortestStr, index + 1);
  34.             }
  35.        
  36.             StringBuilder sb = new StringBuilder();
  37.             sb.Insert(0, shortestStr, counter)
  38.             .Append(longestStr.Replace(shortestStr, string.Empty));
  39.        
  40.             Console.WriteLine(sb);
  41.         }
  42.         else
  43.         {
  44.             string wordOnlySearchCharacters = "";
  45.             string wordWithoutSearchCharacters = "";
  46.             for (int i = 0; i < longestStr.Length; i++)
  47.             {
  48.                 if (shortestStr.Contains(longestStr[i]))
  49.                 {
  50.                     wordOnlySearchCharacters += longestStr[i];
  51.                 }
  52.                 else
  53.                 {
  54.                     wordWithoutSearchCharacters += longestStr[i];
  55.                 }
  56.             }
  57.    
  58.             Console.WriteLine(wordOnlySearchCharacters + wordWithoutSearchCharacters);
  59.         }
  60.         Console.ReadKey(true);
  61.     }
  62. }
Add Comment
Please, Sign In to add comment