Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Program for length of the last word in a string in Go (Golang)

Posted on February 22, 2023February 22, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

The objective is to find the length of the last word in a given string

Example

Input: "computer science"
Output: 7
The last word is science and its length is 7
Input: "computer science is a subject "
Output: 7
The last word is subject and ts length is 7
Input: " "
Output: 0
There is no last word hence answer is zero

Program

Here is the program for the same.

package main
import "fmt"
func lengthOfLastWord(s string) int {
lenS := len(s)
lenLastWord := 0
for i := lenS - 1; i >= 0; {
for i >= 0 && string(s[i]) == " " {
i--
}
if i < 0 {
return 0
}
for i >= 0 && string(s[i]) != " " {
//fmt.Println(i)
//fmt.Println(string(s[i]))
i--
lenLastWord++
}
return lenLastWord
}
return 0
}
func main() {
length := lengthOfLastWord("computer science")
fmt.Println(length)
length = lengthOfLastWord("computer science is a subject")
fmt.Println(length)
length = lengthOfLastWord(" ")
fmt.Println(length)
}

Output

7
7
0

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –All Design Patterns Golang

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP