GoLang Help - To Upper Case

Hi, I’m doing an intensive course that’s teaching us how to create our own packages(functions) on golang. This in mind, we are not allowed to use any packages whatsoever. So, how can I possibly turn lower case characters in a string to uppercase as well as returning the already uppercase characters?

You can:

  1. Start off by researching the algorithm that convert plain a-z to A-Z from the Internet.
  2. Create a function that converts a to A.
  3. Test it with a sentence. Observe all a is converted to A.
  4. If it works, save a copy (or git add) then upgrade it with one character at a time until Z.

You might need to ask your coach are dialect (e.g å-з) included in your conversion. The reason is that you might need to request access for using runes package as there are way too many characters to cover.

Since this is a Go programming homework, I can’t provide code snippets as you need that hands-on development experience (as in, how to be resourceful by extensively using Google Search, handle frustration, pressure management, and deliver within the given time constraint). On the bright side, we can review codes. The point is, you code, not anyone else.

TIPS:

  1. Definitely involves loop and/or slice.
  2. fuzzy logic approach (e.g. if else if or switch).
  3. Alternatively: lookup table approach. If you’re adventurous. Have a look at Unicode-8 bytes table and analyze a way to lookup those characters.
  4. Go packaging example (hint: Titlecase)
  5. If you want an ultimate answer, there is a pattern from the byte table where the conversion can be mathematically executed.
4 Likes

Here are a few ideas that, without providing a direct answer, might help you in the direction of finding an implementation:

  • Check out the go blog section on strings, and pay attention to runes specifically and range loops over strings.
  • For a naïve implementation, take a look at maps. Then take a look at how to check whether a map contains a key. What would happen if you had a map of type map[rune]string and how could you use that combined with what you learned about iterating over the runes in a string?
  • Often if I want to do something similar to something in the stdlib, I look at the code in the stdlib where they have implemented something similar to what I want to do. To that end, the source for strings.Map, strings.ToUpper as well as unicode/letter.ToUpper might be of interest to you if you wanted to pursue the ultimate implementation that @hollowaykeanho hinted at.
3 Likes

package main

import (
“fmt”
)

func main() {
test := “This Is A Test To Upper”
fmt.Println(toUpper(test))
}

func toUpper(str string) string {
var ret_str = “”
for _, chr := range str {
if chr >= 97 && chr <= 122 {
chr -= 32
}
ret_str += fmt.Sprintf("%c", chr)
}
return ret_str
}

1 Like

Hi, thank you very much! I’m now on a quest to build a sudoku solver with golang without any imports except os…… but thanks for all! >.<

1 Like

A bit late. As promised to review your code:

package main

import (
	"fmt"
)

func main() {
	test := "This Is A Test To Upper"
	fmt.Println(toUpper(test))
}

func toUpper(s string) string {
	r := []rune{}
	
	for _, c := range s {
		if c >= 97 && c <= 122 {
			c -= 32
		}
		r = append(r, c)
	}
	return string(r)
}
  1. You do not need to use fmt to perform Sprintf over each character because it is too costly to do so:
    1.1. You’re already looping the string.
    1.2. Sprintf is for heavy duty work like inserting values to placeholder for a string.
    1.3. string(...) type conversion should be suffice to convert []rune type back to string type.

  2. If possible, use meaningful naming convention. Instead of str, just stick to simple s or c for simple function. As you work on larger work in the future, intuitively comprehensible naming convention always help. Avoid hunger syndromes like chr and str whenever possible (See https://golang.org/doc/effective_go).

  3. For return values that requires a variable with initialization (var ret_str = “”) , you can declare it in the function statement like func toUpper(s string) (ret string) {
    3.1. The first benefit is the Go automated documentation. It will be documented clearly that ret is indeed an output control variable.
    3.2. The second benefit is remove the needs to declare the variable again. However, for certain data type (e.g. struct), you still need to initialize it.

  4. Since this is a private function, there is no need to document it.


Continue marking harry’s answer as solution. I’m here to honor my promise to perform code reviews. :upside_down_face: