I am trying to reverse each word of a string and maintaining the uppercase for starting of each word. I don't want to use any build-in library for this. I have written this piece of code but it is giving me below error. Can someone help me what's wrong here? Error
.\reverse_string.go:25:5: cannot assign to word[j] (value of type byte)
.\reverse_string.go:28:5: cannot assign to word[j] (value of type byte)
Code
package main
import (
"fmt"
)
func main() {
//Input - This Is Test
//Output - Siht Si Tset
s:="This Is Test"
revered:=reverseString(s)
fmt.Println(revered)
}
func reverseString(input string) string{
stringArray:=convertToArray(input)
finalWord:=""
for i:=0;i<len(stringArray);i++{
reversedWord:=""
word:=stringArray[i]
for j:=len(word)-1;j>=0;j--{
if word[j]>=97 && word[j]<=122 && j==len(word)-1{
word[j]=word[j]-32
}
if word[j]>=65 && word[j]<=90 && j==0{
word[j]=word[j]+32
}
reversedWord+=string(word[j])
}
if i==len(stringArray){
finalWord=finalWord+reversedWord;
}else{
finalWord=finalWord+reversedWord+" "
}
}
return finalWord
}
func convertToArray(input string)[]string{
stringSlice:=[]string{}
newStr:=""
count:=0
for i:=0;i<len(input);i++{
if string(input[i])==" "{
stringSlice = append(stringSlice, newStr)
newStr=""
}else{
newStr+=string(input[i])
}
count++
}
if count==len(input){
stringSlice=append(stringSlice, newStr)
}
return stringSlice
}