How to concatenate same letter in string array in go [SOLVED]


(Simone Ne) #1

I am currently struggling to write on how to concatenate same letter values onto a string array. In other languages such as Java & C, I can simply use some libraries/functions. But in Go, I am totally lost.

For example,

isSame(“bbcomqdd”);

I want the input to be [bb c o m q dd], not like the following below [bb dd]

A simple code for looking for same letters and append it, then put onto an array string.


(Norbert Melzer) #2

Sorry, I do not really understand what you mean, and all the random asterisks make it hard to copy and run the code.

Could you therefore please either use markdown to fromat your code or provide a link to the playground that has some executable code that shows us what your problem is?

```go
// your code here
```

(Yamil Bracho) #4

I think you mean something like this :

package main

import (
	"fmt"
)

func main() {
  str := "oopdwmeoo"

  prv := []rune(str)[0]
  for _, c := range str {
    fmt.Printf("%c", c)

    if prv != c {
      fmt.Printf(" ")
      prv = c
    }  
  }
}

(Simone Ne) #5

Fundamentally, yes. I’m required to concatenate same values.

but why did your output not oo p d w m e oo?


(Yamil Bracho) #6

Just move the fmt.Printf("%c",c) after the if… my fault :slight_smile:
As a func that returns result as string in https://play.golang.org/p/EFHdEdE39LF


(Simone Ne) #7

Thank you for your kind help. I greatly appreciate it! :smiley:


(Simone Ne) #8

Is there any way that I could make string(arr) onto a string array? [oo, p, d, w, m, e, oo]


(Yamil Bracho) #9

If it is aleadry an array is not needed…
Another way is use an StringBuilder from package string instead of a rune slice…


(Simone Ne) #10

Alright. Thank you.

If you don’t mind me asking, how do I possibly make a contact with you besides here? It seems that you’re profoundly proficient in Golang. I would be feeling much appreciated.


(Yamil Bracho) #11

No problem!!!
I am still learning Go however, :slight_smile:


(Simone Ne) #12

how do I possibly make a contact with you besides here? :slightly_smiling_face:


(Yamil Bracho) #13

My email, yamilbracho@hotmail.com