Showing posts with label reverse. Show all posts
Showing posts with label reverse. Show all posts

Sunday, 28 March 2021

php: strrev: Reverse a string

Signature

strrev ( string $string ) : string

 

Description

Returns the reversed string.

 

Example

$rev_str = strrev($str1);

 

strrev_demo.php

#!/usr/bin/php

<?php

   $str1 = 'Hello';
   $rev_str = strrev($str1);
   
    echo "\$str1: $str1\n";
    echo "\$rev_str: $rev_str\n";
   
?>

 

Output

$./strrev_demo.php 

$str1: Hello
$rev_str: olleH

 

   

Previous                                                    Next                                                    Home

Tuesday, 11 February 2020

Java: Reverse an integer array

Approach 1: Traversing array.
public static void reverse_approach1(int[] arr) {
 if (arr == null || arr.length < 2) {
  return;
 }

 int length = arr.length;
 for (int i = 0; i < length / 2; i++) {
  int temp = arr[i];
  arr[i] = arr[length - i - 1];
  arr[length - i - 1] = temp;
 }

}

Approach 2: Using IntStream.
public static int[] reverse_approach2(int[] arr) {
 if (arr == null || arr.length < 2) {
  return arr;
 }

 return IntStream.rangeClosed(1, arr.length).map(i -> arr[arr.length-i]).toArray();
}

IntArrayUtil.java
package com.smaple.app.utils;

import java.util.stream.IntStream;

public class IntArrayUtil {

 public static void reverse_approach1(int[] arr) {
  if (arr == null || arr.length < 2) {
   return;
  }

  int length = arr.length;
  for (int i = 0; i < length / 2; i++) {
   int temp = arr[i];
   arr[i] = arr[length - i - 1];
   arr[length - i - 1] = temp;
  }

 }
 
 public static int[] reverse_approach2(int[] arr) {
  if (arr == null || arr.length < 2) {
   return arr;
  }

  return IntStream.rangeClosed(1, arr.length).map(i -> arr[arr.length-i]).toArray();
 }
}

IntArrayUtilTest.java

package com.sample.app.utils;

import static org.junit.Assert.*;

import org.junit.Test;

import com.smaple.app.utils.IntArrayUtil;

public class IntArrayUtilTest {

 @Test
 public void reverse_approach1_null() {
  int[] arr = null;
  IntArrayUtil.reverse_approach1(arr);

  assertNull(arr);
 }

 @Test
 public void reverse_approach1_empty() {
  int[] arr = {};
  IntArrayUtil.reverse_approach1(arr);

  assertEquals(arr.length, 0);
 }

 @Test
 public void reverse_approach1_oneElement() {
  int[] arr = { 1 };
  IntArrayUtil.reverse_approach1(arr);

  assertArrayEquals(arr, new int[] { 1 });
 }

 @Test
 public void reverse_approach1_moreElement() {
  int[] arr = { 1, 3, 5, 7, 2, 4, 6, 8 };
  IntArrayUtil.reverse_approach1(arr);

  assertArrayEquals(arr, new int[] { 8, 6, 4, 2, 7, 5, 3, 1 });
 }

 @Test
 public void reverse_approach2_null() {
  int[] arr = null;
  int[] result = IntArrayUtil.reverse_approach2(arr);

  assertNull(result);
 }

 @Test
 public void reverse_approach2_empty() {
  int[] arr = {};
  int[] result = IntArrayUtil.reverse_approach2(arr);

  assertEquals(result.length, 0);
 }

 @Test
 public void reverse_approach2_oneElement() {
  int[] arr = { 1 };
  int[] result = IntArrayUtil.reverse_approach2(arr);

  assertArrayEquals(result, new int[] { 1 });
 }

 @Test
 public void reverse_approach2_moreElement() {
  int[] arr = { 1, 3, 5, 7, 2, 4, 6, 8 };
  int[] result = IntArrayUtil.reverse_approach2(arr);

  assertArrayEquals(result, new int[] { 8, 6, 4, 2, 7, 5, 3, 1 });
 }
}


You may like

Friday, 24 January 2020

Java: Iterate list in reverse order


Approach 1: Traverse the list from end.
private static void printListInReverse_1(List list) {
 System.out.println("Elements of list");
 if(list == null || list.isEmpty()) {
  return;
 }
 
 int size = list.size();
 
 for(int i = size-1; i > -1; i--) {
  System.out.print(list.get(i) + " ");
 }
 
 System.out.println();
}


Approach 2: Using ListIterator.
private static void printListInReverse_2(List list) {
 System.out.println("Elements of list");
 if(list == null || list.isEmpty()) {
  return;
 }
 
 ListIterator listIterator = list.listIterator(list.size());
 while(listIterator.hasPrevious()) {
  System.out.print(listIterator.previous() + " ");
 }
 
 System.out.println();
}


Approach 3: Using Custom iterator.
private static void printListInReverse_3(List list) {
 System.out.println("Elements of list");
 if (list == null || list.isEmpty()) {
  return;
 }

 Iterator iter = new ReverseListIterator(list);

 while (iter.hasNext()) {
  System.out.print(iter.next() + " ");
 }

 System.out.println();
}


Approach 4: By sorting the collection in reverse order.
private static void printListInReverse_4(List list) {
 System.out.println("Elements of list");
 if (list == null || list.isEmpty()) {
  return;
 }

 Collections.reverse(list);

 for (int i = 0; i < list.size(); i++) {
  System.out.print(list.get(i) + " ");
 }

 System.out.println();
}


ReverseListIterator.java
package com.sample.app;

import java.util.Iterator;
import java.util.List;

public class ReverseListIterator<T> implements Iterator<T>, Iterable<T> {

 private final List<T> list;
 private int position;

 public ReverseListIterator(List<T> list) {
  this.list = list;
  this.position = list.size() - 1;
 }

 @Override
 public Iterator<T> iterator() {
  return this;
 }

 @Override
 public boolean hasNext() {
  return position > -1;
 }

 @Override
 public T next() {
  return list.get(position--);
 }

}


App.java
package com.sample.app;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class App {

 private static void printListInReverse_1(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  int size = list.size();

  for (int i = size - 1; i > -1; i--) {
   System.out.print(list.get(i) + " ");
  }

  System.out.println("\n");
 }

 private static void printListInReverse_2(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  ListIterator listIterator = list.listIterator(list.size());
  while (listIterator.hasPrevious()) {
   System.out.print(listIterator.previous() + " ");
  }

  System.out.println("\n");
 }

 private static void printListInReverse_3(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  Iterator iter = new ReverseListIterator(list);

  while (iter.hasNext()) {
   System.out.print(iter.next() + " ");
  }

  System.out.println("\n");
 }

 private static void printListInReverse_4(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  Collections.reverse(list);

  for (int i = 0; i < list.size(); i++) {
   System.out.print(list.get(i) + " ");
  }

  System.out.println();
 }

 public static void main(String[] args) {
  List<Integer> list = Arrays.asList(2, 3, 5, 7, 11);
  printListInReverse_1(list);
  printListInReverse_2(list);
  printListInReverse_3(list);
  printListInReverse_4(list);
 }

}


Run App.java, you will get below messages in console.
Elements of list
11 7 5 3 2 

Elements of list
11 7 5 3 2 

Elements of list
11 7 5 3 2 

Elements of list
11 7 5 3 2


You may like