-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseEachWord
42 lines (37 loc) · 1.25 KB
/
ReverseEachWord
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
using System;
using System.Text;
namespace Algorithms
{
class Program
{
// Create a function that reverses each word in a sentence
// Input: string
// Output: new string with each word reversed
// Assumptions: no punctuation in the string, each
// word separated by spaces
// Casing should remain the same.
static String ReverseEachWord(String input)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
char[] arr = input.ToCharArray();
Array.Reverse(arr);
return new String(arr);
}
static void Main(string [] args)
{
System.Console.WriteLine(ReverseEachWord("Hello World"));
// // returns "dlroW olleH"
System.Console.WriteLine(ReverseEachWord(null));
// // returns a blank
System.Console.WriteLine(ReverseEachWord("racer racecar madam"));
// // returns "racer racecar madam"
System.Console.WriteLine(ReverseEachWord("what can I do today"));
// // returns "yadot od I nac tahw"
System.Console.WriteLine(ReverseEachWord(" "));
// // returns a blank
}
}
}