Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Reverse Words in a sentence

Posted on January 31, 2022January 31, 2022 by admin

Overview

The objective is to reverse words in a given sentence

Examples

Input: "hello world"
Output: "word hello"

Another example. If the input contains a single word then that word will be returned.

Input: "hello"
Output: "hello"

Here is the strategy

  • First, we reverse the entire string. So for “hello world” it becomes
"dlrow olleh"
  • Then we reverse each word
"world hello"
  • We also need to take care of extra spaces at the end or at the start.

Program

Here is the program for the same.

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func reverseWords(s string) string {

	runeArray := []rune(s)
	length := len(runeArray)

	reverseRuneArray := reverse(runeArray)

	for i := 0; i < length; {
		for i < length && string(reverseRuneArray[i]) == " " {
			i++
		}
		if i == length {
			break
		}
		wordStart := i

		for i < length && string(reverseRuneArray[i]) != " " {
			i++
		}

		wordEnd := i - 1

		reverseRuneArray = reverseIndex(reverseRuneArray, wordStart, wordEnd)

	}

	noSpaceString := strings.TrimSpace(string(reverseRuneArray))
	space := regexp.MustCompile(`\s+`)
	return space.ReplaceAllString(noSpaceString, " ")
}

func reverse(s []rune) []rune {
	length := len(s)
	start := 0
	end := length - 1
	for start < end {
		s[start], s[end] = s[end], s[start]
		start++
		end--
	}
	return s
}

func reverseIndex(s []rune, i, j int) []rune {

	start := i
	end := j
	for start < end {
		s[start], s[end] = s[end], s[start]
		start++
		end--
	}
	return s
}

func main() {
	output := reverseWords("hello world")
	fmt.Println(output)

	output = reverseWords("hello")
	fmt.Println(output)
}

Output

world hello
hello
©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme