How to Capitalize the first letter of a word in given text in Golang 1.20

Channel:
Subscribers:
5,740
Published on ● Video Link: https://www.youtube.com/watch?v=55dkYWYXCXI



Category:
Tutorial
Duration: 3:30
11 views
0


In this video we are going to generate source code for the GoLang Program, How to Capitalize the first letter of a word in given text 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.
In this program I have replaced the angled brackets with > or <. once you copy the source code to editor replace it with greater than or less than symbols respectively.

##############SOURCE CODE#########################

package main

import (
"bufio"
"fmt"
"os"
"strings"
)

func capitalize(text string) string {
var capitalizedText string
scanner := bufio.NewScanner(strings.NewReader(text))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := scanner.Text()
if len(word) > 0 {
capitalizedWord := strings.ToUpper(string(word[0])) + word[1:]
capitalizedText += capitalizedWord + " "
}
}
return capitalizedText
}

func main() {
var text string
fmt.Print("Enter a text: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
text = scanner.Text()
capitalizedText := capitalize(text)
fmt.Println("Capitalized text: ", capitalizedText)
}

Below is the explanation for the program:

In this program, the capitalize function takes a text string as input and returns a capitalized version of the text string,
where the first letter of each word is capitalized. The bufio.NewScanner function is used to scan through the input text and separate it into words.
Then, for each word, the first letter is capitalized using the strings.
ToUpper function, and the rest of the word is concatenated to form the capitalized word. Finally, the capitalized words are concatenated to form the capitalized text.

The main function prompts the user to enter a text, reads it from standard input using bufio.NewScanner,
and then calls the capitalize function to capitalize the text.
Finally, the program outputs the capitalized text to standard output.

In this updated version of the program, the bufio.NewScanner function is used to create a scanner that reads input from os.Stdin.
The scanner.Scan() method is then called to read a line of text from the user, which is stored in the text variable.
Finally, the capitalize function is called to capitalize the text, and the capitalized text is output to standard output.

I hope this updated version of the program works properly for you.

#go #goprogramming #golang #golangtutorial #golanguage







Tags:
how to capitalize first letter of word in golang
capitalize letter program in go
Go Programming Tutorial
Go By example
Go Language
go projects
go programming projects
go tutorial
golang tutorial
golang projects
go programming course
learn go
how to run go program
golang videos
golang with chatgpt
coding questions
coding interview questions