-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetMiddleChar.cs
More file actions
43 lines (36 loc) · 1.03 KB
/
Copy pathgetMiddleChar.cs
File metadata and controls
43 lines (36 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace getMiddleChar
{
class Program
{
static void Main(string[] args)
{
string midLetter = GetMiddle("middle");
Console.WriteLine(midLetter);
}
public static string GetMiddle(string s)
{
List<char> charList = new List<char>();
charList.AddRange(s);
double length = charList.Count;
string middle;
if (length % 2 == 0)
{
length = Math.Truncate(length / 2);
int lengthI = Convert.ToInt32(length);
middle = charList[lengthI - 1].ToString() + charList[lengthI].ToString();
}
else
{
length = Math.Truncate(length / 2);
int lengthI = Convert.ToInt32(length);
middle = charList[lengthI].ToString();
}
return middle;
}
}
}