Reverse array elements in Julia – reverse(), reverse!() and reverseind() Methods
The reverse() is an inbuilt function in julia which is used to reverse the specified vector v from start to stop.
Syntax:
reverse(v, start, stop)
or
reverse(A; dims::Integer)Parameters:
- v: Specified vector.
- start: Specified starting value.
- stop: Specified stopping value.
- A: Specified array.
- dims::Integer: Specified dimensions.
Returns: It returns a reversed copy of the vector.
Example 1:
# Julia program to illustrate # the use of reverse() method # Getting the reversed copy of # the specified vector.A = Vector(2:8)println(reverse(A))println(reverse(A, 3, 6))println(reverse(A, 7, 4)) |
Output:
[8, 7, 6, 5, 4, 3, 2] [2, 3, 7, 6, 5, 4, 8] [2, 3, 4, 5, 6, 7, 8]
Example 2:
# Julia program to illustrate # the use of reverse() method # Getting reversed array in # the specified dimensionA = [5 10; 15 20]println(reverse(A, dims = 1))println(reverse(A, dims = 2)) |
Output:
reverse!()
The reverse!() is an inbuilt function in julia which is In-place version of reverse() function.
Syntax:
reverse!(v, start, stop)Parameters:
- v: Specified vector.
- start: Specified starting value.
- stop: Specified stopping value.
Returns: It returns a reversed copy of the vector.
Example:
# Julia program to illustrate # the use of reverse !() method # Getting the reversed copy of # the specified vector.A = Vector(2:8)println(reverse !(A))println(reverse !(A, 3, 6)) |
Output:
[8, 7, 6, 5, 4, 3, 2] [8, 7, 3, 4, 5, 6, 2]
reverseind
The reverseind() is an inbuilt function in julia which is used to return the corresponding index in v so that v[reverseind(v, i)] == reverse(v)[i], where i is the given index.
Syntax:
reverseind(v, i)Parameters:
- v: Specified string.
- i: Specified index.
Returns: It returns the corresponding index in v so that v[reverseind(v, i)] == reverse(v)[i].
Example:
# Julia program to illustrate # the use of reverseind() method # Getting the corresponding index in v# so that v[reverseind(v, i)] == reverse(v)[i]V = reverse("Geeks")for i in 1:length(r) print(V[reverseind("Geeks", i)])end |
Output:


