-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChuckNorris.cs
More file actions
68 lines (62 loc) · 1.98 KB
/
ChuckNorris.cs
File metadata and controls
68 lines (62 loc) · 1.98 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
string MESSAGE = Console.ReadLine();
string binaryResult = string.Empty;
string unaryResult = string.Empty;
foreach (char m in MESSAGE)
{
string lengthCheck = Convert.ToString((int)m, 2);
if (lengthCheck.Length < 7)
{
lengthCheck = new string('0', 7 - lengthCheck.Length) + lengthCheck;
}
binaryResult += lengthCheck;
}
unaryResult = binaryToUnary(binaryResult);
Console.WriteLine(unaryResult.Trim());
//Console.ReadLine();
}
public static string binaryToUnary(string binaryString)
{
char[] binaryArray = binaryString.ToArray();
string unaryResponse = string.Empty;
char lastChar = '\0';
for (int i = 0; i < binaryArray.Length; i++)
{
char currentchar = binaryArray[i];
if (lastChar != '1' && currentchar == '1')
{
unaryResponse += " 0 0";
lastChar = '1';
}
else if (lastChar == '1' && currentchar == '1')
{
unaryResponse += "0";
}
else if (lastChar != '0' && currentchar == '0')
{
unaryResponse += " 00 0";
lastChar = '0';
}
else if (lastChar == '0' && currentchar == '0')
{
unaryResponse += "0";
}
else
{
unaryResponse += "error";
}
}
return unaryResponse;
}
}
}