🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In this guide, we'll walk through the steps to create a Go program capable of counting the vowels in any given string.
2. Program Overview
The structure of our Go program:
1. Gathers a string input from the user.
2. Provide the string for vowels, both lowercase and uppercase.
3. Outputs the number of vowels encountered.
3. Code Program
// The saga begins with the introduction of the main package.
package main
// We recruit the fmt package for its prowess in input and output operations.
import "fmt"
// Function to calculate the number of vowels in a string.
func countVowels(s string) int {
count := 0
for _, char := range s {
switch char {
case 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U':
count++
}
}
return count
}
// The cornerstone of our program, the main function.
func main() {
var inputString string
// Prompting the user to input their desired string.
fmt.Print("Enter your string: ")
fmt.Scanln(&inputString)
// Revealing the number of vowels in the provided string.
fmt.Printf("The number of vowels in \"%s\" is: %d\n", inputString, countVowels(inputString))
}
Output:
As a case in point, if the user submits the string Hello World, the program's response will be: The number of vowels in "Hello World" is: 3
4. Step By Step Explanation
1. Package and Import: Our journey embarks with the package main statement, signifying the program's start. The fmt package is enlisted for smooth input-output handling.
2. Vowel Counting Function: The countVowels function serves as the mainstay for vowel counting. By iterating through each character in the string, the function checks if the character is a vowel (taking into account both lower and upper cases) and subsequently increments a counter.
3. User Interaction & Result Exhibition: The variable inputString is reserved for the user's string. Post fetching the input, the program computes the number of vowels using the countVowels function and then parades the result to the user.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment