遍历一个切片

huangapple go评论74阅读模式
英文:

Ranging over a slice

问题

我的问题是,我只被允许使用PrintRune命令,我必须遍历一个字符串,并逐个打印出任何字符串的字符。

package piscine

import "github.com/01-edu/z01"

func PrintStr(s string) {
	slice := []string{
		s,
	}
	for x, word := range slice {
		z01.PrintRune(rune(word[x]))
	}
}

这是我的代码,它只打印字符串的第一个字符,我该如何使切片继续直到给定字符串的末尾?

英文:

my problem is that Im only allowed to use the command PrintRune, i must range over a string and print one by one the characters of any string

package piscine

import "github.com/01-edu/z01"

func PrintStr(s string) {
	slice := []string{
		s,
	}
	for x, word := range slice {
		z01.PrintRune(rune(word[x]))
	}
}

here's my code, this only prints the first character of the string, how can i make the slice continue until the end of the given string please ?

答案1

得分: 1

这是代码片段:

package piscine

import "github.com/01-edu/z01"

func PrintStr(s string) {
    slice := []string{
        s,
    }
    for _, word := range slice {
        for _, r := range word {
			z01.PrintRune(rune(r))
		}
    }
}
英文:

Here is the code snippets:

package piscine

import "github.com/01-edu/z01"

func PrintStr(s string) {
    slice := []string{
        s,
    }
    for _, word := range slice {
        for _, r := range word {
			z01.PrintRune(rune(r))
		}
    }
}

huangapple
  • 本文由 发表于 2022年3月24日 18:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/71600985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定