How to reverse the case of each character in a string with Go

1 Answer

0 votes
package main

import (
	"fmt"
	"unicode"
)

func reverseCase(s string) string {
	result := []rune(s)

	for i, r := range result {
		if unicode.IsUpper(r) {
			result[i] = unicode.ToLower(r)
		} else if unicode.IsLower(r) {
			result[i] = unicode.ToUpper(r)
		}
	}

	return string(result)
}

func main() {
	input := "ABC++xyz"

	output := reverseCase(input)

	fmt.Println(output)
}



/*
run:

abc++XYZ

*/

 



answered Oct 29, 2024 by avibootz
...