How to resolve the algorithm String matching step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm String matching step by step in the Go programming language

Table of Contents

Problem Statement

Given two strings, demonstrate the following three types of string matching:

Optional requirements:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm String matching step by step in the Go programming language

This Go program demonstrates various string matching operations using the strings package:

  1. Package Import:

    • The program imports the fmt package for input/output operations and the strings package for string manipulation.
  2. match Function:

    • It takes two strings as arguments, first and second, and performs the following operations:

    • Prefix Check:

      • It checks if first starts with second using the strings.HasPrefix function and prints the result.
    • Containment Check:

      • It uses the strings.Index function to find the first occurrence of second within first.
      • If found, it prints that first contains second and indicates its location.
      • It then prints the location of all subsequent occurrences of second using a for loop.
    • Suffix Check:

      • It checks if first ends with second using the strings.HasSuffix function and prints the result.
  3. main Function:

    • It calls the match function with the strings "abracadabra" and "abr" as arguments.

Expected Output:

1. abracadabra starts with abr: true
2. abracadabra contains abr: true,
2.1. at location 0,
2.2. at location 4,
2.2. and that's all
3. abracadabra ends with abr: false

Source code in the go programming language

package main

import (
    "fmt"
    "strings"
)

func match(first, second string) {
    fmt.Printf("1. %s starts with %s: %t\n",
        first, second, strings.HasPrefix(first, second))
    i := strings.Index(first, second)
    fmt.Printf("2. %s contains %s: %t,\n", first, second, i >= 0)
    if i >= 0 {
        fmt.Printf("2.1. at location %d,\n", i)
        for start := i+1;; {
            if i = strings.Index(first[start:], second); i < 0 {
                break
            }
            fmt.Printf("2.2. at location %d,\n", start+i)
            start += i+1
        }
        fmt.Println("2.2. and that's all")
    }
    fmt.Printf("3. %s ends with %s: %t\n",
        first, second, strings.HasSuffix(first, second))
}

func main() {
    match("abracadabra", "abr")
}


  

You may also check:How to resolve the algorithm Wagstaff primes step by step in the Go programming language
You may also check:How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the Factor programming language
You may also check:How to resolve the algorithm Function definition step by step in the AppleScript programming language
You may also check:How to resolve the algorithm String case step by step in the jq programming language
You may also check:How to resolve the algorithm JSON step by step in the Smalltalk programming language