diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-03 20:04:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-03 20:04:57 +0100 |
| commit | e31ed838d2acd186dbad76124afe364bc78608e4 (patch) | |
| tree | 184104e30ca932331ed19debb263b2dd66a03217 | |
| parent | e4d06675f412aa1cc14a0b3685cc5c5e23027224 (diff) | |
| parent | 99c3a68ca7c4bf2b39454050799416353ff26925 (diff) | |
| download | perlweeklychallenge-club-e31ed838d2acd186dbad76124afe364bc78608e4.tar.gz perlweeklychallenge-club-e31ed838d2acd186dbad76124afe364bc78608e4.tar.bz2 perlweeklychallenge-club-e31ed838d2acd186dbad76124afe364bc78608e4.zip | |
Merge pull request #12784 from spadacciniweb/PWC-341
Add ch-2 in Elixir
| -rw-r--r-- | challenge-341/spadacciniweb/elixir/ch-2.exs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-341/spadacciniweb/elixir/ch-2.exs b/challenge-341/spadacciniweb/elixir/ch-2.exs new file mode 100644 index 0000000000..e7a183b845 --- /dev/null +++ b/challenge-341/spadacciniweb/elixir/ch-2.exs @@ -0,0 +1,61 @@ +# Task 2: Reverse Prefix +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str and a character in the given string, $char. +# Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string. +# +# Example 1 +# Input: $str = "programming", $char = "g" +# Output: "gorpmming" +# +# Reverse of prefix "prog" is "gorp". +# +# Example 2 +# Input: $str = "hello", $char = "h" +# Output: "hello" +# +# Example 3 +# Input: $str = "abcdefghij", $char = "h" +# Output: "hgfedcbaj" +# +# Example 4 +# Input: $str = "reverse", $char = "s" +# Output: "srevere" +# +# Example 5 +# Input: $str = "perl", $char = "r" +# Output: "repl" + +defmodule ReverseString do + + def splitStr(str, char) do + s = String.split(str, char, parts: 2) + String.reverse( Enum.at(s, 0) <> char ) <> Enum.at(s, 1) + end + + def out(str, char) do + IO.write( "'" <> str <> "' '" <> char <> "' -> ") + IO.puts( splitStr(str, char) ) + end + +end + +str = "programming" +char = "g" +ReverseString.out( str, char ) + +str = "hello" +char = "h" +ReverseString.out( str, char ) + +str = "abcdefghij" +char = "h" +ReverseString.out( str, char ) + +str = "reverse" +char = "s" +ReverseString.out( str, char ) + +str = "perl" +char = "r" +ReverseString.out( str, char ) |
