-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSwapCase.fs
More file actions
35 lines (30 loc) · 1.19 KB
/
SwapCase.fs
File metadata and controls
35 lines (30 loc) · 1.19 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
namespace Algorithms.Strings
module SwapCase =
type System.Char with
member this.IsUpper(): bool =
match this with
| c when c >= 'A' && c <= 'Z' -> true
| _ -> false
member this.IsLower(): bool =
match this with
| c when c >= 'a' && c <= 'z' -> true
| _ -> false
member this.Lower(): char =
match this with
| c when c >= 'A' && c <= 'Z' -> (char) ((int) this + 32)
| _ -> this
member this.Upper(): char =
match this with
| c when c >= 'a' && c <= 'z' -> (char) ((int) this - 32)
| _ -> this
/// <summary>
/// This function will convert all lowercase letters to uppercase letters and vice versa
/// </summary>
let swapCase (sentence: string): string =
let mutable newString = ""
for character in sentence do
match character with
| c when c.IsUpper() -> newString <- newString + (string) (character.Lower())
| c when c.IsLower() -> newString <- newString + (string) (character.Upper())
| _ -> newString <- newString + (string) character
newString