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:
- Start off by researching the algorithm that convert plain
a-z
toA-Z
from the Internet. - Create a function that converts
a
toA
. - Test it with a sentence. Observe all
a
is converted toA
. - 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:
- Definitely involves loop and/or slice.
- fuzzy logic approach (e.g.
if else if
orswitch
). - Alternatively: lookup table approach. If you’re adventurous. Have a look at Unicode-8 bytes table and analyze a way to lookup those characters.
- Go packaging example (hint:
Titlecase
) - If you want an ultimate answer, there is a pattern from the byte table where the conversion can be mathematically executed.
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 asunicode/letter.ToUpper
might be of interest to you if you wanted to pursue the ultimate implementation that @hollowaykeanho hinted at.
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
}
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! >.<
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)
}
-
You do not need to use
fmt
to performSprintf
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 tostring
type. -
If possible, use meaningful naming convention. Instead of
str
, just stick to simples
orc
for simple function. As you work on larger work in the future, intuitively comprehensible naming convention always help. Avoid hunger syndromes likechr
andstr
whenever possible (See Effective Go - The Go Programming Language). -
For return values that requires a variable with initialization (
var ret_str = “”
) , you can declare it in the function statement likefunc toUpper(s string) (ret string) {
3.1. The first benefit is the Go automated documentation. It will be documented clearly thatret
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. -
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.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.