🎓 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
While Go provides a built-in len() function to fetch the length of a string, understanding how to manually compute this can be an enlightening exercise. In this guide, we'll learn how to create a Go program to find the length of a string without leaning on built-in functions.
2. Program Overview
Our Go program will:
1. Prompt the user to input a string.
2. Traverse the string character by character to compute its length.
3. Exhibit the calculated length to the user.
3. Code Program
// We embark on our journey with the main package declaration.
package main
// The fmt package is summoned for its input-output superpowers.
import "fmt"
// Function to manually compute the length of a string.
func stringLength(str string) int {
length := 0
for range str {
length++
}
return length
}
// The heart and soul of our program - the main function.
func main() {
var inputString string
// A nudge for the user to provide their string.
fmt.Print("Enter your string: ")
fmt.Scanln(&inputString)
// Unveiling the magic of our manual length computation.
fmt.Printf("The length of the string \"%s\" is: %d\n", inputString, stringLength(inputString))
}
Output:
For the sake of illustration, if a user submits the string GoLang, the program's revelation will be: The length of the string "GoLang" is: 6
4. Step By Step Explanation
1. Package and Import: We start by declaring package main, marking the entry point of our program. The fmt package is invoked to manage the program's input and output dynamics.
2. Manual Length Computation Function: The stringLength function iterates over each character in the string using a for-range loop. With every iteration, we increment a counter, ultimately returning its value as the string's length.
3. User Interaction & Result: The variable inputString holds the user's input. After prompting and fetching the string from the user, the program calls the stringLength function and displays the resultant length.
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