Check if a String Is Uppercase in Golang


The challenge

Create a method IsUpperCase to see whether the string is ALL CAPS. For example:

type MyString string
MyString("c").IsUpperCase() == false
MyString("C").IsUpperCase() == true
MyString("hello I AM DONALD").IsUpperCase() == false
MyString("HELLO I AM DONALD").IsUpperCase() == true
MyString("ACSKLDFJSgSKLDFJSKLDFJ").IsUpperCase() == false
MyString("ACSKLDFJSGSKLDFJSKLDFJ").IsUpperCase() == true

In this challenge, a string is said to be in ALL CAPS whenever it does not contain any lowercase letter so any string containing no letters at all is trivially considered to be in ALL CAPS.

The solution in Golang

Option 1:

package solution

import "strings"

type MyString string

func (s MyString) IsUpperCase() bool {
  return s == MyString(strings.ToUpper(string(s)))
}

Option 2:

package solution

type MyString string

func (s MyString) IsUpperCase() bool {
  for _, letter := range s {
    if int(letter) >= 97 && int(letter) <= 122 {
      return false
    }
  }
  return true
}

Option 3:

package solution

import "regexp"

type MyString string

func (s MyString) IsUpperCase() bool {
  r, _ := regexp.Compile(`^[A-Z\s]+$`)
  return r.MatchString(string(s))
}

Test cases to validate our solution

package our_test

import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)

var _ = Describe("Sample Test Cases:", func() {
  It("Should return the correct values", func() {
    Expect(MyString("a").IsUpperCase()).To(Equal(false))
    Expect(MyString("b").IsUpperCase()).To(Equal(false))
    Expect(MyString("c").IsUpperCase()).To(Equal(false))
    Expect(MyString("d").IsUpperCase()).To(Equal(false))
    Expect(MyString("e").IsUpperCase()).To(Equal(false))
    Expect(MyString("f").IsUpperCase()).To(Equal(false))
    Expect(MyString("g").IsUpperCase()).To(Equal(false))
    Expect(MyString("h").IsUpperCase()).To(Equal(false))
    Expect(MyString("i").IsUpperCase()).To(Equal(false))
    Expect(MyString("j").IsUpperCase()).To(Equal(false))
    Expect(MyString("k").IsUpperCase()).To(Equal(false))
    Expect(MyString("l").IsUpperCase()).To(Equal(false))
    Expect(MyString("m").IsUpperCase()).To(Equal(false))
    Expect(MyString("n").IsUpperCase()).To(Equal(false))
    Expect(MyString("o").IsUpperCase()).To(Equal(false))
    Expect(MyString("p").IsUpperCase()).To(Equal(false))
    Expect(MyString("q").IsUpperCase()).To(Equal(false))
    Expect(MyString("r").IsUpperCase()).To(Equal(false))
    Expect(MyString("s").IsUpperCase()).To(Equal(false))
    Expect(MyString("t").IsUpperCase()).To(Equal(false))
    Expect(MyString("u").IsUpperCase()).To(Equal(false))
    Expect(MyString("v").IsUpperCase()).To(Equal(false))
    Expect(MyString("w").IsUpperCase()).To(Equal(false))
    Expect(MyString("x").IsUpperCase()).To(Equal(false))
    Expect(MyString("y").IsUpperCase()).To(Equal(false))
    Expect(MyString("z").IsUpperCase()).To(Equal(false))
    Expect(MyString("false").IsUpperCase()).To(Equal(false))
    Expect(MyString("true").IsUpperCase()).To(Equal(false))
    Expect(MyString("False").IsUpperCase()).To(Equal(false))
    Expect(MyString("True").IsUpperCase()).To(Equal(false))
  })
})