Golang Program to Check Whether the Given String is Pangram


In this article, we will understand different golang examples to check whether a given string is pangram. A statement known as a pangram uses each letter of the alphabet at least once. A pangram is a string in the programming language Golang that contains every letter of the alphabet, regardless of case.

Syntax

strings.ToLower(str)

Using strings in Go (golang), you can change a string's case to lowercase. The strings package's ToLower() function.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

Method 1: Using a Map

In this method, we are going to use map to check Whether the Given String is Pangram. Along with that we are going to use internal function like ToLower and Make functions.

Algorithm

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

  • Step 2 − Create a function isPangram and inside that function use the method ToLower() to change the given string's case to lowercase using

  • Step 3 − To store the frequency of each letter in the string, create a map using make function.

  • Step 4 − Go through the string's characters one at a time.

  • Step 5 − Increase the character's frequency in the map if it is a letter and repeat this process for each letter (from "a" to "z") in the alphabet.

  • Step 6 − Verify if the current letter on the map has a frequency greater than zero and return false if any letter's frequency is zero.

  • Step 7 − Return true if the sum of all letter frequencies is greater than zero.

  • Step 8 − By counting the frequency of each letter and determining if it is greater than zero, this algorithm determines whether every letter of the alphabet is present in the string. The string is regarded as a pangram if every letter has a positive frequency.

Example

In this example, we will see how to check whether a given string is pangram using map

package main
import (
   "fmt"
   "strings"
)

func isPangram(str string) bool {
   // make all characters lowercase
   str = strings.ToLower(str)
   // create a map to store the frequency of each character
   m := make(map[rune]int)
   // iterate over each character in the string
   for _, c := range str {
      // if the character is a letter, increase its frequency in the map
      if c >= 'a' && c <= 'z' {
         m[c]++
      }
   }
   // check if the frequency of each letter is greater than zero
   for i := 'a'; i <= 'z'; i++ {
      if m[i] == 0 {
         return false
      }
   }
   return true
}

func main() {
   str := "I am a frontend developer"
   fmt.Println("The pangram is checked as:")
   if isPangram(str) {
      fmt.Println(str, "is a pangram")
   } else {
      fmt.Println(str, "is not a pangram")
   }
}

Output

The pangram is checked as:
I am a frontend developer is not a pangram

Method 2: Using a Set

In this method, we are going to use set to check Whether the Given String is Pangram.

Algorithm

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

  • Step 2 − Create a function isPangram and inside that function to store the string's unique characters, create a set.

  • Step 3 − Use the strings.ToLower() to change the given string's case to lowercase.

  • Step 4 − Go through the string's characters one at a time.

  • Step 5 − Add the character to the set if it is a letter and verify that the set is 26 pieces long.

  • Step 6 − Return true if there are 26 distinct characters in the set and return false if there are fewer than 26 unique characters in the set.

  • Step 7 − By adding unique characters to a set and determining whether the set's length is 26, this algorithm determines whether every letter of the alphabet is included in the string. If it is, true is returned; otherwise, false. If every letter of the alphabet is included in the string, there will be 26 different characters in the set.

Example

In this example, we will check whether the given string is pangram using a set

package main
import (
   "fmt"
   "strings"
)

func isPangram(str string) bool {
   // create a set to store the unique characters in the string using make function
   set_create := make(map[rune]bool)
   // make all the character lowercase
   str = strings.ToLower(str)
   // iterate over each character in the string
   for _, c := range str {
      // if the character is a letter, add it to the set
      if c >= 'a' && c <= 'z' {
         set_create[c] = true
      }
   }
   // check if the set contains 26 unique characters
   return len(set_create) == 26
}

func main() {
   str := "I am a frontend developer"
   if isPangram(str) {
      fmt.Println(str, "is a pangram")
   } else {
      fmt.Println(str, "is not a pangram")
   }
}

Output

I am a frontend developer is not a pangram

Conclusion

We executed the program of checking whether a given string is pangram using two examples. In the first example, we used map and in the second example, we used set to execute the program.

Updated on: 20-Feb-2023

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements