From 00d333acac4462192af2c9d6172f8910b9aa78ec Mon Sep 17 00:00:00 2001 From: lareii Date: Wed, 26 Jul 2023 15:35:02 +0300 Subject: [PATCH] add reverse string algorithm --- README.md | 1 + string/reverse.jule | 9 +++++++++ test.jule | 5 +++++ 3 files changed, 15 insertions(+) create mode 100644 string/reverse.jule diff --git a/README.md b/README.md index 9c7d2a9..63bf30a 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Algorithms and data structures implemented in the Jule programming language. - [Is Digit](./string/is_digit.jule) - [Lower Case](./string/lower_case.jule) - [Upper Case](./string/upper_case.jule) + - [Reverse](./string/reverse.jule) ## Contributing diff --git a/string/reverse.jule b/string/reverse.jule new file mode 100644 index 0000000..8af3df1 --- /dev/null +++ b/string/reverse.jule @@ -0,0 +1,9 @@ +pub fn reverse(mut string: str): str { + let mut i: uint = 0 + + for i < string.len/2; i++ { + string[i], string[string.len-i-1] = string[string.len-i-1], string[i] + } + + ret string +} \ No newline at end of file diff --git a/test.jule b/test.jule index 693607b..3299299 100644 --- a/test.jule +++ b/test.jule @@ -15,6 +15,7 @@ use string::{ is_digit, upper_case, lower_case, + reverse, } static mut TESTS: TestStack = TestStack.new() @@ -88,6 +89,10 @@ fn add_tests_string() { TESTS.add("lower_case", fn(): bool { ret lower_case("FOo") == "foo" }) + + TESTS.add("reverse", fn(): bool { + ret reverse("FooBar") == "raBooF" + }) } fn main() {