3 ms·
I wasn't sure whether he meant words as in the data size unit or words as in English words. However, since we are assuming it's ASCII, 1 char = 1 byte. Reverse
by chethan 18y ago
I wasn't sure whether he meant words as in the data size unit or words as in English words. However, since we are assuming it's ASCII, 1 char = 1 byte.
Reverse the order of the bytes in each word.
Reverse the order of bytes in the file.
[edit]If you're doing 1 word = the unit of size, the first step isn't necessary since all words will obviously be the same length. Just go through and swap the 1st word with the Nth, the 2nd with the (N-1)th, etc.[/edit]
Since you are doing in-place swapping, you aren't using any extra memory aside from a 1-byte (word) temporary register.
If you want to be really clever, you can use the XOR swap algorithm and not use the byte at all (at the cost of more CPU use).
This will work in O(N) time.