-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.mc
More file actions
124 lines (111 loc) · 3.13 KB
/
strings.mc
File metadata and controls
124 lines (111 loc) · 3.13 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// MimerCode Sample - String Utilities
// Copyright 2026 Components4Developers - Kim Bo Madsen
// Run with: MimerCodeRunner strings.mc
program StringUtils
// --- Reverse a string ---
func ReverseStr(const S: String) -> String
local Result: String = ''
for I := Length(S) - 1 downto 0 do
Result += Copy(S, I, 1)
end
return Result
end
// --- Check if palindrome ---
func IsPalindrome(const S: String) -> Boolean
local Lower: String
Lower := LowerCase(S)
return Lower = ReverseStr(Lower)
end
// --- Count occurrences of a character ---
func CountChar(const S: String, const C: String) -> Integer
local Count: Integer = 0
for I := 0 to Length(S) - 1 do
if Copy(S, I, 1) = C then
Count += 1
end
end
return Count
end
// --- Simple Caesar cipher ---
func Caesar(const Text: String, Shift: Integer) -> String
local Result: String = ''
local Ch: Integer
local Base: Integer
for I := 0 to Length(Text) - 1 do
Ch := Ord(Copy(Text, I, 1))
if (Ch >= 65) and (Ch <= 90) then
// Uppercase A-Z
Base := ((Ch - 65) + Shift) mod 26
if Base < 0 then
Base += 26
end
Result += Chr(Base + 65)
elif (Ch >= 97) and (Ch <= 122) then
// Lowercase a-z
Base := ((Ch - 97) + Shift) mod 26
if Base < 0 then
Base += 26
end
Result += Chr(Base + 97)
else
Result += Chr(Ch)
end
end
return Result
end
// --- Word count ---
func WordCount(const S: String) -> Integer
local Count: Integer = 0
local InWord: Boolean = False
local Ch: String
for I := 0 to Length(S) - 1 do
Ch := Copy(S, I, 1)
if (Ch = ' ') or (Ch = Chr(9)) then
InWord := False
else
if not InWord then
Count += 1
InWord := True
end
end
end
return Count
end
// --- Demos ---
WriteLn('=== String Utilities ===')
WriteLn('')
WriteLn('Reverse:')
WriteLn(f' "Hello" -> "{ReverseStr("Hello")}"')
WriteLn(f' "MimerCode" -> "{ReverseStr("MimerCode")}"')
WriteLn('')
WriteLn('Palindrome check:')
local Words: TArray<String>
Words := ['racecar', 'hello', 'madam', 'world', 'level', 'kayak']
for W in Words do
if IsPalindrome(W) then
WriteLn(f' "{W}" is a palindrome')
else
WriteLn(f' "{W}" is NOT a palindrome')
end
end
WriteLn('')
WriteLn('Character counting:')
local Sentence: String = 'Mississippi'
WriteLn(f' "{Sentence}" has {CountChar(Sentence, "s")} lowercase s characters')
WriteLn(f' "{Sentence}" has {CountChar(Sentence, "i")} lowercase i characters')
WriteLn('')
WriteLn('Caesar cipher:')
local Plain: String = 'Hello World'
local Encrypted: String
Encrypted := Caesar(Plain, 3)
local Decrypted: String
Decrypted := Caesar(Encrypted, -3)
WriteLn(f' Plain: "{Plain}"')
WriteLn(f' Encrypted: "{Encrypted}"')
WriteLn(f' Decrypted: "{Decrypted}"')
WriteLn('')
WriteLn('Word count:')
local Text: String = 'The quick brown fox jumps over the lazy dog'
WriteLn(f' "{Text}"')
WriteLn(f' Words: {WordCount(Text)}')
end