Golang program to compare two strings lexicographically


In Golang, lexicographic comparison refers to comparing strings in a manner similar to how they would be compared in a dictionary where the words are listed alphabetically. This means that the initial character of each string is used to compare the individual characters of two strings in order. The comparison process continues until a difference is discovered or one of the strings' ends is reached if the initial few characters are the same. The string that appears first in the lexical order is regarded as being "less than" the other. Let’s see via examples how to execute this program.

Method 1: Using User-defined function

In this example, the for loop in this program is used to repeatedly go through the characters in the two strings and compare each one individually. If a discrepancy is discovered, the loop is broken and the algorithm determines which string's largest character. If there is no discrepancy, the software determines which string is shorter. The program outputs "the two strings are lexicographically equal" if both strings have the same length and all character’s match.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Example:s and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function define two string variables to be compared with name hello and alexa.

  • Step 3 − Find the minimum length of the two strings

  • Step 4 − Use a for loop to iterate through the characters of the two strings, comparing them one by one.

  • Step 5 − If a mismatch is found, break the loop and check which string's character is larger.

  • Step 6 − If no mismatch is found, check which string is shorter.

  • Step 7 − If both strings are the same length and all character’s match, print that the two strings are lexicographically equal.

  • Step 8 − Use a helper function min(x,y) which returns the min of two integers.

  • Step 9 − This algorithm is more efficient than the previous one as it only iterates up to the minimum length of the two strings instead of iterating over the entire string. This results in faster execution time.

Example

In this example, we are going use a user-defined function to compare two strings lexicographically

package main
import "fmt"

func main() {
   mystring1 := "hello"  //create string1
   fmt.Println("The value of string1 is:", mystring1)
   mystring2 := "alexa"  //create string2
   fmt.Println("The value of string2 is:", mystring2)
   minLength := min(len(mystring1), len(mystring2))
   i := 0
   for i < minLength {         //use for loop to compare strings
      if mystring1[i] != mystring2[i] {
         break
      }
      i++
   }
   if i == minLength {
      if len(mystring1) > len(mystring2) {
         fmt.Printf("%s is lexicographically after %s\n", mystring1, mystring2)
      } else if len(mystring1) < len(mystring2) {
         fmt.Printf("%s is lexicographically before %s\n", mystring1, mystring2)
      } else {
         fmt.Printf("%s is lexicographically equal to %s\n", mystring1, mystring2)
      }
   } else {
      if mystring1[i] > mystring2[i] {
         fmt.Printf("%s is lexicographically after %s\n", mystring1, mystring2)
      } else {
         fmt.Printf("%s is lexicographically before %s\n", mystring1, mystring2)
      }
   }
}

func min(x, y int) int {
   if x < y {
      return x
   }
   return y
}

Output

The value of string1 is: hello
The value of string2 is: alexa
hello is lexicographically after alexa

Method 2: Using strings.Compare() function

In this method, the internal function returns an integer indicating their relative order: a negative value if the first string is lexicographically before the second, zero if they are equal, and a positive value if the first string is lexicographically after the second. Let’s see through the Example: and the algorithm to see how it executes.

Syntax

strings.Compare()

The Compare() function is used to lexicographically compare two strings. It gives back an integer number that shows how the two strings are related to one another. A value of 0 denotes equality between the strings, a value below 0 denotes lexicographic inferiority of the first string relative to the second string, and a value above 0 denotes lexical superiority of the first string.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Example:s and fmt helps in formatting input and output.

  • Step 2 − Create a function main and in that function to compare, define two string variables mystr1 and mystr2.

  • Step 3 − Use the Compare() function to lexicographically compare the two strings. If the first string is lexicographically before the second, this function produces a negative value; if they are equal, it returns a zero; and if the first string is lexicographically after the second, it returns a positive number.

  • Step 4 − To determine the value returned by the strings, use an if-else expression.

  • Step 5 − Printing that the first string is lexicographically before the second is necessary if the value is negative.

  • Step 6 − Print that the two strings are lexicographically equal if the value is zero.

  • Step 7 − Printing that the first string comes after the second lexicographically is done if the value is positive.

  • Step 8 − The print statement is executed using fmt.Println() function where ln means new line.

Example

In this example, the program uses the strings.Compare() function to compare the two strings lexicographically.

package main
import (
   "fmt"
   "strings"
)

func main() {
   mystr1 := "hello" //create string1
   fmt.Println("The value of string1 is:", mystr1)
   mystr2 := "alexa"  //create string2
   fmt.Println("The value of string2 is:", mystr2)

   output := strings.Compare(mystr1, mystr2) //compare strings based on compare function

   if output < 0 {
      fmt.Printf("%s is lexicographically before %s\n", mystr1, mystr2)
   } else if output == 0 {
      fmt.Printf("%s is lexicographically equal to %s\n", mystr1, mystr2)
   } else {
      fmt.Printf("%s is lexicographically after %s\n", mystr1, mystr2)
   }
}

Output

The value of string1 is: hello
The value of string2 is: alexa
hello is lexicographically after alexa

Conclusion

In this article, we have executed two programs to to compare two strings lexicographically using Go programming language. In the first example, we have used a user-defined function of For loop and in the second example, we have used an internal String function called as strings.Compare().

Updated on: 20-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements