-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathCSharp.cs
63 lines (52 loc) · 1.72 KB
/
CSharp.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
62
63
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Text formatting */
/* Difficulty: Easy */
/* Date solved: 13.11.2018 */
/* */
/****************************************/
using System;
using System.Linq;
using System.Text.RegularExpressions;
public class Solution
{
public static void Main()
{
//Read input.
string text = Console.ReadLine().ToLower().Trim();
//Remove excessive spaces.
text = Regex.Replace(text, @"\s{2,}", " ");
//Remove spaces before and after punctuations.
text = Regex.Replace(text, @"\s?\p{P}\s?", (match) =>
{
return match.Value.Trim();
});
//Remove repeated punctuations.
text = Regex.Replace(text, @"\p{P}+", (match) =>
{
return match.Value.Trim()[0].ToString();
});
//First letter uppercase.
text = string.Join(".", text.Split('.').Select(s => StringToPascalCase(s)));
//Add spaces after punctuations.
text = Regex.Replace(text, @"\p{P}", (match) =>
{
return match.Value + " ";
});
//Output formatted text.
Console.WriteLine(text.Trim());
}
//Converts any string to pascal case.
private static string StringToPascalCase(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
char[] chars = s.ToCharArray();
chars[0] = char.ToUpper(chars[0]);
return new string(chars);
}
}