Home Software Engineering Test if a String is Uppercase in Golang

Test if a String is Uppercase in Golang

0
Test if a String is Uppercase in Golang

[ad_1]

The problem

Create a technique IsUpperCase to see whether or not the string is ALL CAPS. For instance:

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

On this problem, a string is claimed to be in ALL CAPS at any time when it doesn’t comprise any lowercase letter so any string containing no letters in any respect is trivially thought of to be in ALL CAPS.

The answer in Golang

Possibility 1:

package deal answer

import "strings"

kind MyString string

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

Possibility 2:

package deal answer

kind MyString string

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

Possibility 3:

package deal answer

import "regexp"

kind MyString string

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

Take a look at instances to validate our answer

package deal our_test

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

var _ = Describe("Pattern Take a look at Circumstances:", func() {
  It("Ought to return the right values", func() {
    Anticipate(MyString("a").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("b").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("c").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("d").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("e").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("f").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("g").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("h").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("i").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("j").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("okay").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("l").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("m").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("n").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("o").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("p").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("q").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("r").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("s").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("t").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("u").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("v").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("w").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("x").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("y").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("z").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("false").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("true").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("False").IsUpperCase()).To(Equal(false))
    Anticipate(MyString("True").IsUpperCase()).To(Equal(false))
  })
})

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here