A simple web tool that reverses the order of words or letters in a string of text.
- Reverse Words: Reorder the words in a string in reverse. For example, "hello world" becomes "world hello".
- Reverse Letters: Reverse the letters in each word while maintaining the word order. For example, "hello world" becomes "olleh dlrow".
- Copy to Clipboard: Easily copy the transformed text to the clipboard with a single click.
The reverseWords
function employs a stack-based algorithm to reverse the order of words in a string. Stacks operate on a Last In, First Out (LIFO) principle, making them ideal for this use case.
- The function starts by trimming leading and trailing spaces.
- It then iterates through the string, grouping characters into words and pushing each word onto the stack when a space is encountered.
- After the entire string has been processed, words are popped from the stack to form the reversed string, effectively reversing the word order.
The time complexity of the reverseWords
function is O(n), with n being the length of the input string. This linear complexity arises because each character is processed exactly once.
The reverseLetters
function also has a time complexity of O(n), as it involves splitting the input into words and then reversing each word's characters before joining them back together.
Due to the immutability of strings in JavaScript, concatenation operations create new strings, which can be inefficient for very large strings. In such cases, alternative methods that avoid creating multiple intermediate strings may offer improved performance.
Simply type or paste text into the input area and click on the "Reverse Words" or "Reverse Letters" button as needed. The reversed text will appear in the output area below the buttons. Use the copy icon to copy the text to your clipboard.
This small button with a clipboard icon allows users to copy the reversed text with ease. It uses the Clipboard API to copy text to the user's clipboard.
- HTML5
- CSS3
- JavaScript
- Material Symbols Outlined for the copy icon