🎓 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
String operations constitute a significant portion of various computing tasks. One common operation is converting a string to lowercase, which helps in text normalization, especially in search operations, data analytics, and more. Though Go offers built-in functionality for this, manually implementing it can be a great learning exercise. In this blog post, we will learn how to write a Go program to convert a string to lowercase without using the built-in method.
2. Program Overview
This Go program aims to:
1. Take a string from the user.
2. Manually transform this string into its lowercase counterpart.
3. Showcase the transformed string to the user.
3. Code Program
// declaration of the main package.
package main
// The fmt package for input and output functionalities.
import "fmt"
// Function to transform a string to lowercase.
func toLowerCase(str string) string {
result := []rune(str) // Transform string to a slice of runes for individual character manipulation.
// Iterate through each rune in the string.
for i, r := range result {
if r >= 'A' && r <= 'Z' {
result[i] = r + 32 // Convert uppercase ASCII character to its lowercase counterpart.
}
}
return string(result) // Return the manipulated rune slice as a string.
}
// The main function, serving as the epicenter of our program.
func main() {
var inputString string
// Prompt the user for input.
fmt.Print("Enter a string: ")
fmt.Scanln(&inputString)
// Display the transformed string.
fmt.Printf("Lowercase version: %s\n", toLowerCase(inputString))
}
Output:
For example, should the user proffer the string HELLO, the program will declare: Lowercase version: hello
4. Step By Step Explanation
1. Package and Import: Our program begins with the package main declaration. For all I/O operations, the fmt package is our companion.
2. Lowercase Transformation Function: The toLowerCase function provides logic to convert the given string to lowercase.
3. Main Function: The main function takes the input from the user, calls the toLowerCase function, and prints the result.
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