gpt-4o-2024-05-13· openai
WildChat-4.8M
In bash: Task Develop a script that takes a string as an argument and returns its…
- User
In bash: Task Develop a script that takes a string as an argument and returns its reverse version, changing uppercase letters to lowercase and back
Check ./loops1.sh "Hello World"
DLROw OLLEh
./loops1.sh "bash"
HSAB
./loops1.sh "EPAM"
mape
- Assistant
Here's a Bash script (
reverse_case.sh) that will take a string as an argument, reverse the string, and swap the case of each character:#!/bin/bash # Function to reverse a string reverse_string() { local str="$1" local reversed="" for (( i=${#str}-1; i>=0; i-- )); do reversed+=${str:$i:1} done echo "$reversed" } # Function to swap the case of each character in a string swap_case() { local str="$1" local swapped="" for (( i=0; i<${#str}; i++ )); do char="${str:$i:1}" if [[ $char =~ [A-Z] ]]; then swapped+="${char,,}" # Convert to lowercase elif [[ $char =~ [a-z] ]]; then swapped+="${char^^}" # Convert to uppercase else swapped+="$char" fi done echo "$swapped" } # Check if an argument is provided if [ $# -eq 0 ]; then echo "Usage: $0 <string>" exit 1 fi input_string="$1" reversed_string=$(reverse_string "$input_string") final_output=$(swap_case "$reversed_string") echo "$final_output"
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.