0

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
}
Pratyush
  • 41
  • 5
  • 1
    Strings in Go are **immutable**, you cannot therefore do `word[j] = x` if `word` is a `string`. You can convert a `string` to a `byte` slice (i.e. `bs := []byte(s)`) and modify the contents of the byte slice. Then, once you're finished with your conversions, you can convert the byte slice back to a string `s = string(bs)`. – mkopriva Nov 05 '22 at 16:51
  • https://go.dev/ref/spec#String_types: "A string type represents the set of string values. A string value is a (possibly empty) sequence of bytes. The number of bytes is called the length of the string and is never negative. **Strings are immutable: once created, it is impossible to change the contents of a string.** The predeclared string type is string; it is a defined type." – mkopriva Nov 05 '22 at 16:55

1 Answers1

0
package main

import (
    "fmt"
)

func main() {
    //Input - This Is Test
    //Output -Siht Si Tset

    s:="This Is Test"
    reveredString:=reverseString(s)
    fmt.Println(string(reveredString))
    
}

func reverseString(input string) []byte{
    finalString:=[]byte{}
    word:=[]byte{}
    for i:=0;i<len(input);i++{
        if (input[i]>=65 && input[i]<=90) || (input[i]>=97 && input[i]<=122){
            word=append(word, input[i])
            if i==len(input)-1{
                word=convertCase(reverseWord(word))
                finalString=append(finalString, word...)
            }
        }else{
            word=convertCase(reverseWord(word))
            finalString=append(finalString, word...)
            finalString=append(finalString, input[i])
            word=[]byte{}
        }
    }
    return finalString
}

func reverseWord(wordBytes []byte)[]byte{
    for i,j:=0,len(wordBytes)-1;i<j;i,j=i+1,j-1{
        wordBytes[i],wordBytes[j]=wordBytes[j],wordBytes[i]
    }
    return wordBytes
}
func convertCase(wordBytes []byte)[]byte{
    for k,v:=range wordBytes{
        if v>=65 && v<=90 && k==len(wordBytes)-1{
            wordBytes[k]+=32
        }
        if v>=97 && v<=122 && k==0{
            wordBytes[k]-=32
        }
    }
    return wordBytes
}

Pratyush
  • 41
  • 5