How to delete vowels from string in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to delete vowels from string using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
##############SOURCE CODE#########################
package main
import (
"fmt"
"strings"
)
func main() {
// Example input string
inputString := "Hello, World!"
// Convert the input string to lowercase
inputString = strings.ToLower(inputString)
// Define a string containing vowels
vowels := "aeiou"
// Replace all vowels in the input string with an empty string
outputString := strings.Map(func(r rune) rune {
if strings.ContainsRune(vowels, r) {
return -1
}
return r
}, inputString)
// Output the result
fmt.Println(outputString)
}
Below is the explanation for the program:
In this program, we first convert the input string to lowercase, to ensure that we capture all possible vowels.
Then we define a string containing all the vowels, and use the strings.Map() function to iterate over each character in the input string.
If the character is a vowel, we replace it with an empty string (i.e. delete it). If it is not a vowel, we keep it in the output string.
#go #goprogramming #golang #golangtutorial #golanguage