-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathunary.cs
61 lines (56 loc) · 1.44 KB
/
unary.cs
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
using System;
class Program
{
static string ToBinary(string text)
{
string binaryText = "";
foreach (char character in text)
{
binaryText += Convert.ToString(character, 2).PadLeft(7, '0');
}
return binaryText;
}
static string ToUnary(string text)
{
string unaryText = "";
bool prevDigit = false; // False = 0, True = 1
// Handle first character
if (text.Length >= 1)
{
if (text[0] == '0')
{
unaryText += "00 0";
}
else
{
unaryText += "0 0";
prevDigit = true;
}
}
for (int i = 1; i < text.Length; i++)
{
if (text[i] == '0' && prevDigit)
{
unaryText += " 00 0"; // Switch from 1 to 0
prevDigit = false;
}
else if (text[i] == '1' && !prevDigit)
{
unaryText += " 0 0"; // Switch from 0 to 1
prevDigit = true;
}
else
{
unaryText += "0"; // Repeat digit
}
}
return unaryText;
}
static void Main(string[] args)
{
string text = Console.ReadLine();
string binaryText = ToBinary(text);
string unaryText = ToUnary(binaryText);
Console.WriteLine(unaryText);
}
}