> For the complete documentation index, see [llms.txt](https://anton-veselskyi.gitbook.io/codding-problems-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anton-veselskyi.gitbook.io/codding-problems-solutions/codewars/7-kyu/moves-in-squared-strings-i.md).

# Moves in squared strings (I)

### [Moves in squared strings (I)](https://www.codewars.com/kata/56dbe0e313c2f63be4000b25)

This kata is the first of a sequence of four about "Squared Strings".

You are given a string of `n` lines, each substring being `n` characters long: For example:

`s = "abcd\nefgh\nijkl\nmnop"`

We will study some transformations of this square of strings.

* Vertical mirror:

  vert\_mirror (or vertMirror or vert-mirror)

  ```
  vert_mirror(s) => "dcba\nhgfe\nlkji\nponm"
  ```
* Horizontal mirror:

  hor\_mirror (or horMirror or hor-mirror)

  ```
  hor_mirror(s) => "mnop\nijkl\nefgh\nabcd"
  ```

or printed:

```
vertical mirror   |horizontal mirror   
abcd --> dcba     |abcd --> mnop 
efgh     hgfe     |efgh     ijkl 
ijkl     lkji     |ijkl     efgh 
mnop     ponm     |mnop     abcd
```

## Task:

* Write these two functions

and

* high-order function `oper(fct, s)` where
  * fct is the function of one variable f to apply to the string `s`

    (fct will be one of `vertMirror, horMirror`)

## Examples:

```
s = "abcd\nefgh\nijkl\nmnop"
oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm"
oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd"
```

## Note:

The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests".

## Bash Note:

The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests".

Forthcoming katas will study other transformations.

### Solutions

#### 💲 Shell

```
#!/bin/bash

#will set internal field separator (IFS) to ','
IFS=','

vertMirror()
{
    arr=() #bash array
    for field 
    do
      arr+=( $(rev <<< $field) )
    done

    str=""
    for (( i=0; i < ${#arr[*]}; ++i))
    do
      str+=${arr[$i]}
      str+=$'\r'
    done

    echo $str
}

horMirror()
{
    arr=("$@") #bash array

    str=""
    for (( i=${#arr[*]}-1; i >= 0; --i))
    do
      str+=${arr[$i]}
      str+=$'\r'
    done

    echo $str
}

oper()
{
  $1 "${@:2}"
}

oper $1 $2
IFS=' '
```
