Below is the implementation of the message encryption:
1234567891011121314151617181920212223242526272829303132function reverseString(s) { // Convert string to array (strings are immutable in JavaScript) const chars = s.split(''); // Two pointers approach let left = 0; let right = chars.length - 1; while (left < right) { // Swap characters const temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; // Move pointers towards the middle left++; right--; } // Convert array back to string return chars.join('');} // Alternative approach using built-in methodsfunction reverseStringAlt(s) { return s.split('').reverse().join('');} // Test casesconsole.log(reverseString("hello")); // "olleh"console.log(reverseString("Secure Message")); // "egasseM eruceS"console.log(reverseString("Agent 007")); // "700 tnegA"
Let's break down the implementation:
Implement the message encryption solution in different programming languages.
Below is the implementation of the message encryption in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132function reverseString(s) { // Convert string to array (strings are immutable in JavaScript) const chars = s.split(''); // Two pointers approach let left = 0; let right = chars.length - 1; while (left < right) { // Swap characters const temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; // Move pointers towards the middle left++; right--; } // Convert array back to string return chars.join('');} // Alternative approach using built-in methodsfunction reverseStringAlt(s) { return s.split('').reverse().join('');} // Test casesconsole.log(reverseString("hello")); // "olleh"console.log(reverseString("Secure Message")); // "egasseM eruceS"console.log(reverseString("Agent 007")); // "700 tnegA"
Set up two pointers: one at the beginning of the string (left) and one at the end (right).
Swap the characters at the left and right positions using a temporary variable.
Increment the left pointer and decrement the right pointer to move them toward the center.
Continue swapping and moving pointers until they meet or cross each other.
Return the modified string, which is now reversed.
split('')
to convert string to arrayjoin('')
to convert array back to stringsplit('').reverse().join('')
list(s)
to convert string to list''.join(chars)
to convert list back to stringa, b = b, a
s[::-1]
(slice notation)toCharArray()
to convert string to char arraynew StringBuilder(s).reverse().toString()
std::reverse(s.begin(), s.end())
from <algorithm>
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the message encryption problem using a different approach than shown above.
An empty string reversed is still an empty string.
A string with a single character remains unchanged when reversed.
A palindrome reads the same forward and backward, so reversing has no effect.
Below is the implementation of the message encryption:
1234567891011121314151617181920212223242526272829303132function reverseString(s) { // Convert string to array (strings are immutable in JavaScript) const chars = s.split(''); // Two pointers approach let left = 0; let right = chars.length - 1; while (left < right) { // Swap characters const temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; // Move pointers towards the middle left++; right--; } // Convert array back to string return chars.join('');} // Alternative approach using built-in methodsfunction reverseStringAlt(s) { return s.split('').reverse().join('');} // Test casesconsole.log(reverseString("hello")); // "olleh"console.log(reverseString("Secure Message")); // "egasseM eruceS"console.log(reverseString("Agent 007")); // "700 tnegA"
Let's break down the implementation:
Implement the message encryption solution in different programming languages.
Below is the implementation of the message encryption in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132function reverseString(s) { // Convert string to array (strings are immutable in JavaScript) const chars = s.split(''); // Two pointers approach let left = 0; let right = chars.length - 1; while (left < right) { // Swap characters const temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; // Move pointers towards the middle left++; right--; } // Convert array back to string return chars.join('');} // Alternative approach using built-in methodsfunction reverseStringAlt(s) { return s.split('').reverse().join('');} // Test casesconsole.log(reverseString("hello")); // "olleh"console.log(reverseString("Secure Message")); // "egasseM eruceS"console.log(reverseString("Agent 007")); // "700 tnegA"
Set up two pointers: one at the beginning of the string (left) and one at the end (right).
Swap the characters at the left and right positions using a temporary variable.
Increment the left pointer and decrement the right pointer to move them toward the center.
Continue swapping and moving pointers until they meet or cross each other.
Return the modified string, which is now reversed.
split('')
to convert string to arrayjoin('')
to convert array back to stringsplit('').reverse().join('')
list(s)
to convert string to list''.join(chars)
to convert list back to stringa, b = b, a
s[::-1]
(slice notation)toCharArray()
to convert string to char arraynew StringBuilder(s).reverse().toString()
std::reverse(s.begin(), s.end())
from <algorithm>
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the message encryption problem using a different approach than shown above.
An empty string reversed is still an empty string.
A string with a single character remains unchanged when reversed.
A palindrome reads the same forward and backward, so reversing has no effect.