Golang Program to Count no. of Numerical Digits in a String


A Golang string is a sequence of characters created using quotes. Strings are immutable here, which means once created cannot be modified. Here we will work on strings to find numerical digits present in them.In this article, we will write a Go language program to count no. of numerical digits in a string.

Demonstration

This demonstration explains that “helloalexa234567playthesong” is an input string and it contains six numeric digits, the role of the program is to tally all the numeric digits available in the provided string.

  • Input string − helloalexa234567playthesong

  • Number of Numeric Digit − 6

Syntax

unicode.IsDigit()

This function belongs to the unicode package. It is used to check whether a Unicode code point represents a digit or not. If it represents a digit true is returned otherwise false is returned.

regexp.MustCompile()

This function is the part of the regexp package where compiled regex expression is returned.

regex.FindAllString()

This function is used to find the matching pattern of the expression in the string. It takes two arguments the string and -1 which implies that there is no limit to the matches found.

func range(variable)

The range function is used to iterate over any data type. To use this, we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.

funclen(v Type) int

The len() function is used to get the length of any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Algorithm

  • Step 1 − This program imports two packages fmt and Unicode, the former is used in formatting of the input and the output and the latter is used in the finding the numerical digit

  • Step 2 − Create a main function and in that function create a string and assign it to mystr

  • Step 3 − Print the string on the console using Println function from the fmt package

  • Step 4 − In this step, assign the variable count zero and run a loop on the string created above and check using Unicode package whether the numerical digit is present in the string

  • Step 5 − If the numerical digit is present in the string increment the count variable and keep incrementing until the loop is terminated

  • Step 6 − Finally, the count of numerical digits will be obtained and stored inside the count variable

  • Step 7 − The count variable will be printed on the console using Println function from the fmt package where ln refers to the new line

Example 1

In this example, we will create a main function in which numerical digits will be counted using the unicode package where count will be incremented after every numerical digit is found using IsDigit function.

package main

import (
   "fmt"
   "unicode"
)


func main() {
   mystr := "helloalexa666424playthesong46"
   fmt.Println("The string in which numerical digit is to be counted is:", mystr)
   count := 0
   for _, char := range mystr {
      if unicode.IsDigit(char) {
         count++
      }
   }
   fmt.Println("Number of numerical digits in the string are:", count)
}

Output

The string in which numerical digit is to be counted is: helloalexa666424playthesong46
Number of numerical digits in the string are: 8

Example 2

In this example, the if conditional will be used to check whether the character in the string is any numerical digit. If the condition is satisfied, the count variable will be incremented.

package main

import (
	"fmt"
)

func main() {
   mystr := "helloalexa234567playthesong"
   fmt.Println("The string created here is:", mystr)
   count := 0
   for _, char := range mystr {
      if char >= '0' && char <= '9' {
         count++
      }
   }
   fmt.Println("Number of numerical digits:", count)
}

Output

The string created here is: helloalexa234567playthesong
Number of numerical digits: 6

Example 3

In this example, regexp package will be used to find the numerical digit in the string. Finally, the length of the resulting slice will be counted and we will obtain the total numerical digits in the string.

package main

import (
   "fmt"
   "regexp"
)
func main() {
   mystr := "helloalexa234568playthesong"
   fmt.Println("The string from which numerical digits is to be counted:", mystr)
   regex := regexp.MustCompile(`\d`)
   count := len(regex.FindAllString (mystr, -1))
   fmt.Println("Number of numerical digits in the string are:", count)
}

Output

The string from which numerical digits is to be counted: helloalexa234568playthesong
Number of numerical digits in the string are: 6

Conclusion

We compiled and executed the program of counting numerical digits in a string using three examples. In the first example, we used the Unicode package with IsDigit method, in the second example, we used if conditional and in the third example, we used the regexp package to execute the program.

Updated on: 20-Jul-2023

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements