Rookie question on range and on conversion of int to UTF character

2 issues - first one is: why does the “%v \n” command convert the string to a UTF character after the str declaration but not in the for loop. Secondly, why doesn’t this program exit properly given that I have the range function in there?
Thanks to anyone who take the time to respond - I am a rank beginner to programming trying to figure out a few things.
Best,
Charles


//the object of this program is to take a string of utf8 codes and
//convert them into the rune represented by the codes
//only useful for roman alphabet as it assumes that every slice that starts with a 1
//is a 3 character slice
import "fmt"

var k = 0
var j = 2

func main() {
	str := "218526535870434111373812685"
	fmt.Printf("%v \n", string(65))
	
	for range str {
		if str[k:k+1] == "1" {
			fmt.Printf("%v \n", string(str[k:j+1]))
			k = k + 3
			j = j + 3
			//	fmt.Println(str[k:j])
		} else {
			fmt.Printf("%v \n", string(str[k:j]))
			k = k + 2
			j = j + 2

		}
	}
}

Your first print is of string(65), where 65 is the codepoint for “A” so you get the string A. Your next prints are of substrings of your long string, for example str[0:2] which is "21", which is already a string and thus printed as 21.

If you want to print the codepoint corresponding to 21 you need to parse the string "21" as an integer and then convert that to a string.

1 Like

Thank you! Very helpful!

…why doesn’t this program exit properly…?

The code produces a “slice bounds out of range” panic. (https://play.golang.org/p/r-uswaokMx)
The problem here: for range moves through the string byte by byte, whereas k and j increase in steps of 2 or 3, respectively. At some point, either k, j, or j+1 is larger than len(str), which is when one of the slice operations on str fails.

Since your code moves through the string by increasing k and j, I would suggest replacing the range operator with a loop with a condition, like for k,j := 0,2; k < len(str) && j < len(str)-1;, to ensure the loop stops before the slice operations go past the end of str.

1 Like

Thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.