gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
Develop a bash script that takes a string as an argument and returns its reverse…
- User
Develop a bash script that takes a string as an argument and returns its reverse version, changing uppercase letters to lowercase and back
- Assistant
Certainly! Below is the bash script that reverses the input string and switches uppercase letters to lowercase and vice versa:
#!/bin/bash # Function to reverse the string and switch the case of each character reverse_and_switch_case() { local input="$1" local length=${#input} local reversed_and_switched="" for (( i=$length-1; i>=0; i-- )); do char="${input:$i:1}" if [[ $char =~ [A-Z] ]]; then reversed_and_switched+="${char,,}" elif [[ $char =~ [a-z] ]]; then reversed_and_switched+="${char^^}" else reversed_and_switched+="$char" fi done echo "$reversed_and_switched" } # Check if an argument is provided if [ -z "$1" ]; then echo "Usage: $0 <string>" exit 1 fi # Call the function with the input string reverse_and_switch_case "$1"How to Use This Script
- Save the script to a file, e.g.,
reverse_case.sh. - Make the script executable by running:
chmod +x reverse_case.sh - Run the script with a string argument:
./reverse_case.sh "YourStringHere"
Example
./reverse_case.sh "HelloWorld"This would output:
DLROwOLLEhIn this example, the string "HelloWorld" is reversed to "dlroWolleH" and each letter's case is switched, resulting in "DLROwOLLEh".
- Save the script to a file, e.g.,
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.