reverse

Last edit

Summary: {{{ awk

Changed:

< {{{

to

> {{{ awk


The awk extraction and reporting language does not provide a [[builtin?]] function for reversing strings. However, it is possible to achieve this by creating a user defined [[function?]]. The following example demonstrates the definition of a reverse function and its invocation:

function reverse(s) {
  revs = ""
  for (l = length(s); l > 0 ; l--) {
    c = substr(s, l, 1)
    revs = revs c
  }
  return revs
}

# Call the reverse function to reverse our string
BEGIN {
  print reverse("anut fo raj a rof tun A")
}