Solved Reversing the order of characters

July 20, 2018 at 12:02:06
Specs: Linux Suse, T5240
Hi guys,

Does anyone know how can I reverse the order of number bellow ?

from:
8160503173

to:
1806051337


Thanks,


See More: Reversing the order of characters

Reply ↓  Report •

✔ Best Answer
July 20, 2018 at 18:59:02
I am assuming you are looking for something to execute from the linux command line? This awk script should work. Let me know if you have any questions


#!/bin/bash

# this awk script transposes each pair of characters
echo 8160503173|awk ' { 
for(i=1; i<=length($0); i++)
   {
   ch=substr($0,i,1)
   ram = i % 2
   if (ram == 0) # even, transpose the characters
      {
      arr[i+1] = ch
      arr[i+2] = buffer
      }
   else  # odd, save the character
     buffer = ch
   }
# print out array arr one character at a time
for(p in arr)
   printf("%c", arr[p])
print ""
} '

message edited by nails



#1
July 20, 2018 at 12:16:55
Do you want a shell script or a Batch, C++, Java, Python, Excel, Ruby, PHP, etc. etc. ??????

MIKE

http://www.skeptic.com/


Reply ↓  Report •

#2
July 20, 2018 at 13:57:51
Here is an Excel solution:

=VALUE(MID(A1,2,1)&MID(A1,1,1)&MID(A1,4,1)&MID(A1,3,1)&MID(A1,6,1)&MID(A1,5,1)&MID(A1,8,1)&MID(A1,7,1)&MID(A1,10,1)&MID(A1,9,1))

MIKE

http://www.skeptic.com/


Reply ↓  Report •

#3
July 20, 2018 at 15:10:01
I know virtually nothing significant re’ Excel formulae, and follow the posts relating to Excel formulae - which invariably get responses from Mike... and Derby... and a few others - so as to learn a little (actually a lot) more. Often I can follow the discussion and even understand the answers...

The Excel example given here (#2) by Mike... has me totally in the dark. It might as well be Zorian code compiled using technology recovered from the Roswell crashed flying saucer...; even worse perhaps early Sumerian or pre Indus code...

Perhaps you (Mike...) can explain what that formula is about; what is the sequence etc.; what each step actually does, means (please)?

au secours, m’aidez,.... svp...


Reply ↓  Report •

Related Solutions

#4
July 20, 2018 at 16:38:29
TRVLR

The solution is a sequence of MID() functions.
This function picks out a piece of text from the middle of a text entry and
needs to know at what point it should start, and how many characters to pick.

=VALUE(
MID(A1,2,1)&
MID(A1,1,1)&
MID(A1,4,1)&
MID(A1,3,1)&
MID(A1,6,1)&
MID(A1,5,1)&
MID(A1,8,1)&
MID(A1,7,1)&
MID(A1,10,1)&
MID(A1,9,1))

So MID(A1,2,1) says
From cell A1, go to the Second Character and fetch One character.
For the OP's string 8160503173 that would be the number 1

MID(A1,1,1) says
From cell A1, go to the First Character and fetch One character.
For the OP's string 8160503173 that would be the number 8

MID(A1,4,1) says
From cell A1, go to the Fourth Character and fetch One character.
For the OP's string 8160503173 that would be the number 0

and so on thru the complete string

The Ampersand Symbol, &, joins the individual numbers
and we get 1806051337

The MID() function has an unfortunate side effect
in that it converts numbers to TEXT so
the VALUE() function converts a piece of text, which resembles a number, into an actual value.

MIKE

http://www.skeptic.com/


Reply ↓  Report •

#5
July 20, 2018 at 18:59:02
✔ Best Answer
I am assuming you are looking for something to execute from the linux command line? This awk script should work. Let me know if you have any questions


#!/bin/bash

# this awk script transposes each pair of characters
echo 8160503173|awk ' { 
for(i=1; i<=length($0); i++)
   {
   ch=substr($0,i,1)
   ram = i % 2
   if (ram == 0) # even, transpose the characters
      {
      arr[i+1] = ch
      arr[i+2] = buffer
      }
   else  # odd, save the character
     buffer = ch
   }
# print out array arr one character at a time
for(p in arr)
   printf("%c", arr[p])
print ""
} '

message edited by nails


Reply ↓  Report •

#6
July 21, 2018 at 04:03:02
@ Mike... Many taks for the explanation...

Seems to me you could do this for a living, or at least a past time, instead of playing gloff, volley ball, bowling etc...

trvlr...


Reply ↓  Report •

#7
July 31, 2018 at 12:47:12
@Nails,
Sorry for the delaying to reply it, I was travelling.
It was what i am looking for, thanks !!!!

@Mike,
Thanks, good to know about Excel and your explanation.

message edited by surfistadesampa


Reply ↓  Report •

#8
August 1, 2018 at 05:55:01
surfistadesampa :

You are welcome, and thank you for the recognition and the kind words.

nails

message edited by nails


Reply ↓  Report •

Ask Question