Golang program to capitalize first character of each word in a string


A string in Golang is a collection of characters. Since strings in Go are immutable, they cannot be modified after they have been produced. Concatenating or adding to an existing string, however, enables the creation of new strings. A built-in type in Go, the string type can be used in a variety of ways much like any other data type.

Syntax

strings.Join(words,” ”)

A slice of strings can be joined together with a separator using the join method. Two arguments are required by the function: a slice of strings, and a separator string. It gives back a single string that is made up of all the slice elements joined together and divided by the separator.

strings.Fields(str)

A slice of the substrings is returned by the Fields() function, which divides a string into substrings depending on whitespace characters. The whitespace characters used to divide the string are absent from the returned slice.

strings.Title(word)

The first letter of each word in a string is converted to uppercase using the Title() function, while the remaining letters are converted to lowercase.

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Step 1 − Create a package main and declare fmt and strings package

  • Step 2 − Create a function main

  • Step 3 − Use internal function to slice the first character from the word and capitalize it.

  • Step 4 − Using join or append function combine the capitalized character with the word

  • Step 5 − Print the output

Example 1

In this example we will see how to capitalize first character of each word using built-in functions Fields(), Join() and Title(). The output will be the first letter of the word capitalized on the console.

package main
import (
	"fmt"
	"unicode"
)

func main() {
	mystr := "hello, alexa!"  //create a string 
	fmt.Println("The original string given here is:", mystr)
	var output []rune    //create an output slice
	isWord := true
	for _, val := range mystr {
		if isWord && unicode.IsLetter(val) {  //check if character is a letter convert the first character to upper case
			output = append(output, unicode.ToUpper(val))
			isWord = false
		} else if !unicode.IsLetter(val) {
			isWord = true
			output = append(output, val)
		} else {
			output = append(output, val)
		}
	}
	fmt.Println("The string after its capitalization is:")
	fmt.Println(string(output))  //print the output with first letter as capitalized
}

Output

The original string given here is: hello, alexa!
The string after its capitalization is:
Hello, Alexa!

Example 2

In this example we will learn how to capitalize first character of each word in a string by converting a string to slice of runes.

package main
import (
	"fmt"
	"unicode"
)

func main() {
	mystr := "hello, alexa!"  //create a string 
	fmt.Println("The original string given here is:", mystr)
	var output []rune    //create an output slice
	isWord := true
	for _, val := range mystr {
		if isWord && unicode.IsLetter(val) {  //check if character is a letter convert the first character to upper case
			output = append(output, unicode.ToUpper(val))
			isWord = false
		} else if !unicode.IsLetter(val) {
			isWord = true
			output = append(output, val)
		} else {
			output = append(output, val)
		}
	}
	fmt.Println("The string after its capitalization is:")
	fmt.Println(string(output))  //print the output with first letter as capitalized
}

Output

The original string given here is: hello, alexa!
The string after its capitalization is:
Hello, Alexa!

Conclusion

We executed the program of capitalizing the first character of each word of a string using two examples. In the first example we used built-in functions and in the second example we used the Unicode package to capitalize the character.

Updated on: 01-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements