From 99c3a68ca7c4bf2b39454050799416353ff26925 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 2 Oct 2025 17:08:32 +0200 Subject: Add ch-2 in Elixir --- challenge-341/spadacciniweb/elixir/ch-2.exs | 61 +++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 challenge-341/spadacciniweb/elixir/ch-2.exs 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 ) -- cgit