Advertisement
NelIfandieva

ReturnMax_Mathods

Jan 22nd, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. string input = Console.ReadLine();
  2.            
  3.  
  4.             if(input == "int")
  5.             {
  6.                 int intOne = int.Parse(Console.ReadLine());
  7.                 int intTwo = int.Parse(Console.ReadLine());
  8.  
  9.                 int max = ReturnMax(intOne, intTwo);
  10.             }
  11.             else if(input == "char")
  12.             {
  13.                 char charOne = char.Parse(Console.ReadLine());
  14.                 char charTwo = char.Parse(Console.ReadLine());
  15.  
  16.                 char biggerChar = ReturnMax(charOne, charTwo);
  17.             }
  18.             else if(input == "string")
  19.             {
  20.                 string stringOne = Console.ReadLine();
  21.                 string stringTwo = Console.ReadLine();
  22.  
  23.                 string max = ReturnMax(stringOne, stringTwo);
  24.             }
  25.         }
  26.  
  27.         private static int ReturnMax(int intOne, int intTwo)
  28.         {
  29.             int max = 0;
  30.  
  31.             if(intOne > intTwo)
  32.             {
  33.                 max = intOne;
  34.             }
  35.             else if(intOne < intTwo)
  36.             {
  37.                 max = intTwo;
  38.             }
  39.  
  40.             return max;
  41.         }
  42.  
  43.         private static char ReturnMax(char charOne, char charTwo)
  44.         {
  45.             char max = ' ';
  46.  
  47.             if(charOne > charTwo)
  48.             {
  49.                 max = charOne;
  50.             }
  51.             else if(charOne < charTwo)
  52.             {
  53.                 max = charTwo;
  54.             }
  55.  
  56.             return max;
  57.         }
  58.  
  59.         private static string ReturnMax(string stringOne, string stringTwo)
  60.         {
  61.             // "Helloz"
  62.             // "Mellow"
  63.  
  64.  
  65.             string max = "";
  66.  
  67.             int shorterStringLength = 0;
  68.  
  69.             if(stringOne.Length < stringTwo.Length)
  70.             {
  71.                 shorterStringLength = stringOne.Length;
  72.             }
  73.             else if (stringTwo.Length < stringOne.Length)
  74.             {
  75.                 shorterStringLength = stringTwo.Length;
  76.             }
  77.  
  78.             bool maxFound = false;
  79.  
  80.             for (int i = 0; i < shorterStringLength; i++)
  81.             {
  82.                 char currentInStringOne = stringOne[i];
  83.                 char currentInStringTwo = stringTwo[i];
  84.  
  85.                 if(currentInStringOne > currentInStringTwo)
  86.                 {
  87.                     max = stringOne;
  88.                     maxFound = true;
  89.                     break;
  90.                 }
  91.                 else if(currentInStringTwo > currentInStringOne)
  92.                 {
  93.                     max = stringTwo;
  94.                     maxFound = true;
  95.                     break;
  96.                 }
  97.             }
  98.  
  99.             if(maxFound == false)
  100.             {
  101.                 max = "empty output";
  102.             }
  103.             return max;
  104.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement