-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (58 loc) · 1.58 KB
/
Program.cs
File metadata and controls
70 lines (58 loc) · 1.58 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Program
{
static int[] Bubblesort(int[] arr)
{
for(int i = 0; i < arr.Length - 1; i++)
{
for(int j = 0; j < arr.Length - i - 1; j++)
{
if(arr[j] > arr[j + 1])
{
(arr[j], arr[j + 1]) = (arr[j + 1], arr[j]);
}
}
}
return arr;
}
static int ReverseInt(int num)
{
int reversed = 0;
while(num != 0)
{
int digit = num % 10;
if(reversed > int.MaxValue / 10 || reversed < int.MinValue / 10)
return 0;
if(reversed == int.MaxValue / 10 && digit > 7 || reversed == int.MinValue / 10 && digit < -8)
return 0;
reversed = reversed * 10 + digit;
num = num / 10;
}
return reversed;
}
static string ReverseString(string str)
{
var chars = str.ToArray();
int left = 0;
int right = chars.Length - 1;
while(left < right)
{
(chars[left], chars[right]) = (chars[right], chars[left]);
left++;
right--;
}
return new string(chars);
}
static string ReverseStringRecursive(string str)
{
if(str == "")
return str;
return ReverseString(str.Substring(1)) + str[0];
}
static void Main(string[] args)
{
Console.WriteLine(ReverseInt(1201));
Console.WriteLine(ReverseString("Hello"));
Console.WriteLine(ReverseStringRecursive("Hello"));
}
}