- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Java Program to Capitalize the first character of each word in a String
- How to capitalize the first letter of each word in a string using JavaScript?
- Write a Java program to capitalize each word in the string?
- Python program to capitalize each word's first letter
- How to print the first character of each word in a String in Java?
- Golang program to iterate through each character of string
- Golang program to print first letter of each word using regex
- Capitalize last letter and Lowercase first letter of a word in Java
- Print first letter of each word in a string in C#
- Golang program to find the frequency of character in a string
- Golang Program to Convert Character to String
- Using a recursive function to capitalize each word in an array in JavaScript
- Print first letter of each word in a string using C# regex
- Write a java program to reverse each word in string?
- Write a java program to tOGGLE each word in string?
