How to count the number of uppercase and lowercase letters in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to Count the number of uppercase and lowercase letters 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"
"unicode"
"bufio"
"os"
)
func main() {
// Prompt the user to enter a string
fmt.Print("Enter a string: ")
var input string
//fmt.Scan(&input)
//This will scan full string
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input = scanner.Text()
// Count the number of uppercase and lowercase letters
upperCount := 0
lowerCount := 0
for _, char := range input {
if unicode.IsUpper(char) {
upperCount++
} else if unicode.IsLower(char) {
lowerCount++
}
}
// Print the result
fmt.Printf("Uppercase letters: %d\n", upperCount)
fmt.Printf("Lowercase letters: %d\n", lowerCount)
}
Below is the explanation for the program:
In this program, we first prompt the user to enter a string using the fmt.Scan function.
We then use a for loop to iterate over each character in the string,
and we use the unicode.IsUpper and unicode.IsLower functions to check whether each character is uppercase or lowercase, respectively.
We maintain separate counters for the number of uppercase and lowercase letters,
and increment them accordingly whenever we encounter a matching character.
Finally, we print the counts of uppercase and lowercase letters using the fmt.Printf function,
which allows us to format the output string using the %d verb to print integers.
#go #goprogramming #golang #golangtutorial #golanguage