
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Uppercase in Go
Strings are a built-in data type that represents sequences of characters in Golang. They are defined using double quotes (") and can contain any valid Unicode characters. In this article we will write a go language program to convert a string to uppercase. Here we will see the usage of both for loop and library functions to carry on the respective conversion.
Method, 1: Using user-defined function
In this method, we are going to create a user-defined function to convert a string into upper-case. The Steps to perform that are explained below ?
Algorithm
Step 1 ? First, we need to import the fmt package.
Step 2 ? Then, create a function to convert a string to uppercase. The function accepts the string as the argument. Convert the string obtained into byte variable and use a for loop to iterate over the byte.
Step 3 ? Now, if the characters forming the string are in lowercase then take that particular character of the byte and subtract from it the difference between a and A which turns out to be 32.
Step 4 ? After that subtract the difference from the values of the byte array and store them back to the byte array.
Step 5 ? Return the byte array. start the main() function. Inside the main() initialize a string and print it on the screen.
Step 6 ? Now call the ToUpper() function and pass the string as argument to it. store the result obtained in a variable called result and print the result on the screen.
Example
In this example, we will write a go language program to convert a string to uppercase by using an external function.
package main import ( "fmt" ) //function to convert characters to upper case func ToUpper(s string) string { b := []byte(s) for i, c := range b { if c >= 'a' && c <= 'z' { b[i] = c - ('a' - 'A') } } return string(b) } func main() { var input string = "convert this string to uppercase" fmt.Println("The given string is:\n", input) var output string = ToUpper(input) fmt.Println() fmt.Println("String obtained by converting the above string to uppercase is:\n", output) }
Output
The given string is: convert this string to uppercase String obtained by converting the above string to uppercase is: CONVERT THIS STRING TO UPPERCASE
Method 2: Using internal functions
In this method, we are going to use the internal function of golang to convert a normal string to upper-case string. The syntax used in this method are explained below ?
Syntax
func ToUpper(r rune) rune
ToUpper() function present in unicode package is used to convert a given string to uppercase. It takes the rune character as an argument and returns a rune in uppercase by converting the given rune to uppercase.
func ToUpper(str string) string
ToUpper() function is present in strings package and is used to convert a given string to uppercase. The function accepts the given string as argument and returns the final string after converting it to upper case.
func ToTitle(str string) string
ToTitle() function is present in strings package and is used to convert a string to uppercase. The function takes string to be converted as an argument to it and returns the string after converting its case to upper.
Algorithm
Step 1 ? First, we need to import the fmt and Unicode package.
Step 2 ? Now, create the toUpper() function that accepts the string whose case is to be changed and returns the string. Inside the function we are first converting the string obtained into rune and then using for loop to iterate over the rune array.
Step 3 ? If the character of the array turns out to be in lowercase then we need to convert it to upper case by using Unicode.Upper() function by passing that character as argument to it.
Step 4 ? Now, store the character one by one in the initialized array and return the final array.
Step 5 ? Now, start the main() function. Here initialize a variable of type string and store value to it. further print the array on the screen.
Step 6 ? Call the ToUpper() function by passing the string as argument to it and store the result in a variable and print it on the screen.
Example 1
In this example we will the Unicode package to convert a given string to uppercase in go programming language. In this function we will use the characters of the string one by one to convert them to uppercase.
package main import ( "fmt" "unicode" ) func ToUpper(s string) string { a := []rune(s) for i, c := range a { if unicode.IsLower(c) { a[i] = unicode.ToUpper(c) } } return string(a) } func main() { var input string = "convert this string to uppercase" fmt.Println("The given string is:\n", input) var output string = ToUpper(input) fmt.Println() fmt.Println("The string obtained by converting above string to uppercase is:\n", output) }
Output
The given string is: convert this string to uppercase The string obtained by converting above string to uppercase is: CONVERT THIS STRING TO UPPERCASE
Example 2
In this example we will write a go language program to convert a string to uppercase by using the ToUpper() function present in strings package.
package main import ( "fmt" "strings" ) func main() { var input string = "convert this string to uppercase" fmt.Println("The given string is:\n", input) var output string = strings.ToUpper(input) fmt.Println() fmt.Println("The string obtained by converting the given string to uppercase is:\n", output) }
Output
The given string is: convert this string to uppercase The string obtained by converting the given string to uppercase is: CONVERT THIS STRING TO UPPERCASE
Example 3
In this example we will use a go language program to convert a string to uppercase by using ToTitle() function.
package main import ( "fmt" "strings" ) func main() { var str string = "This is a string to be converted" fmt.Println("The given string is:\n", str) fmt.Println() var res string = strings.ToTitle(str) fmt.Println("The string obtained by converting the above string to uppercase is:\n", res) }
Output
The given string is: This is a string to be converted The string obtained by converting the above string to uppercase is: THIS IS A STRING TO BE CONVERTED
Conclusion
We have successfully compiled and executed a go language program to convert a string to uppercase along with examples.