Rostring
Rostring is an exercice that ask you to move the first word at the end of a sentence.
Functionality
PrintStr function
The PrintStr function takes a string as an input and prints it to the console using the z01.PrintRune function.
It's a simplified fmt.Print function to print strings.
func PrintStr(s string) {
for i := 0; i < len(s); i++ {
z01.PrintRune(rune(s[i]))
}
z01.PrintRune('\n')
}
Spaces function
The Spaces function takes a string as an input and returns a new string with consecutive spaces removed.
func Spaces(s string) string {
result := ""
spaces := false
for i := 0; i < len(s); i++ {
if s[i] != ' ' {
result += string(s[i])
spaces = true
} else if s[i] == ' ' && spaces {
result += " "
spaces = false
}
}
return result
}
spaces := false
if s[i] != ' ' {
result += string(s[i])
spaces = true
}
else if s[i] == ' ' && spaces {
result += " "
spaces = false
}
How it works
The Rostring function checks if the input string has exactly one argument. If not, it prints a newline character and returns.
if len(os.Args) != 2 {
z01.PrintRune('\n')
return
}
word := Spaces(os.Args[1])
end := 0
for i := 0; i < len(word); i++ {
if word[i] == ' ' {
end = i
i = len(word)
}
}
- We print the sentence that will start at the end of the first word so we don't repeat the first word 2 times.
- We then add a space between them.
- And then we print only the first word, which is the sentence that stops at the index end that we got earlier.
- To be sure that we don't have any consecutive spaces we use the Spaces function on that string.
PrintStr(Spaces(word[end:]) + " " + word[:end])
Full code
package main
import (
"os"
"github.com/01-edu/z01"
)
// function that take care of consecutive spaces
func Spaces(s string) string {
result := ""
spaces := false
for i := 0; i < len(s); i++ {
if s[i] != ' ' {
result += string(s[i])
spaces = true
} else if s[i] == ' ' && spaces {
result += " "
spaces = false
}
}
return result
}
// function that will allow us to print a string
func PrintStr(s string) {
for i := 0; i < len(s); i++ {
z01.PrintRune(rune(s[i]))
}
z01.PrintRune('\n')
}
// main function
func Rostring() {
if len(os.Args) != 2 {
z01.PrintRune('\n')
return
}
word := Spaces(os.Args[1])
end := 0
for i := 0; i < len(word); i++ {
if word[i] == ' ' {
end = i
i = len(word)
}
}
PrintStr(Spaces(word[end:]) + " " + word[:end])
}