Skip to content

Instantly share code, notes, and snippets.

@timetorock
Created June 11, 2019 16:28
Show Gist options
  • Save timetorock/b08021eb77c4f1b868b45b5ecafe416e to your computer and use it in GitHub Desktop.
Save timetorock/b08021eb77c4f1b868b45b5ecafe416e to your computer and use it in GitHub Desktop.
Vowels reverse
package main
import (
"errors"
"fmt"
)
const (
minStringLength = 3
maxStringLength = 10
)
type VowelsConverter struct {
bytes []byte
}
func (vc VowelsConverter) Validate() error {
if len(vc.bytes) < minStringLength || len(vc.bytes) > maxStringLength {
return errors.New("invalid string length")
}
return nil
}
func (vc VowelsConverter) PrintState() {
fmt.Println(string(vc.bytes))
}
func (vc VowelsConverter) isVowel(letter byte) bool {
switch letter {
case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':
return true
default:
}
return false
}
func (vc VowelsConverter) VowelsReverse() []byte {
j := len(vc.bytes) - 1
for i := range vc.bytes {
if !vc.isVowel(vc.bytes[i]) {
continue
}
if j <= i {
return vc.bytes
}
for j > 0 {
if !vc.isVowel(vc.bytes[j]) {
j = j - 1
continue
}
vc.bytes[i], vc.bytes[j] = vc.bytes[j], vc.bytes[i]
j = j - 1
break
}
}
return vc.bytes
}
func main() {
//bytes, err := ioutil.ReadAll(os.Stdin)
//if err != nil {
// fmt.Println(err)
// return
//
//}
bytes := []byte("abcdefoui")
converter := VowelsConverter{
bytes: bytes,
}
converter.PrintState()
err := converter.Validate()
if err != nil {
fmt.Println(err)
return
}
converter.VowelsReverse()
converter.PrintState()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment