Like this-Using go lang in go lang using (for loop and range fun)
Given a String S, reverse the string without reversing its individual words. Words are separated by dots.
Input : i.like.this.program.very.much Output: much.very.program.this.like.i
package main
import (
"fmt"
"strings"
)
func main() {
str := " i.like.this.program.very.much"
words := strings.Split(str, " ")
len := len(words)
for i := 0; i<len/2; i {
tmp := words[len-i-1]
words[len-i-1] = words[i]
words[i] = tmp
}
str = strings.Join(words," ")
fmt.Println(str)
}
CodePudding user response:
Something like this:
https://goplay.tools/snippet/EEFOAYGfQ6z
package main
import (
"fmt"
"strings"
)
func main() {
s := "i.like.this.program.very.much"
r := reverseWords(s)
fmt.Println(s)
fmt.Println(r)
}
func reverseWords(s string) string {
words := strings.Split(s, ".")
reversedWords := reverse(words)
r := strings.Join(reversedWords, ".")
return r
}
func reverse(arr []string) []string {
for i, j := 0, len(arr)-1; i < j; {
arr[i], arr[j] = arr[j], arr[i]
i
j--
}
return arr
}
CodePudding user response:
A String S, reverse the string without reversing its individual words. Words are separated by dots.
package main
import (
"fmt"
"strings"
)
func main() {
str := "i.like.this.program.very.much"
words := strings.Split(str, " ")
len := len(words)
for i := 0; i<len/2; i {
tmp := words[len-i-1]
words[len-i-1] = words[i]
words[i] = tmp
}
str = strings.Join(words," ")
fmt.Println(str)
}
but o/p is disply:- i.like.this.program.very.much but i want
likw this :-much.very.program.this.like.i ....plz help thanku ....