Go Program to Check if a String Contains Only Digits

Introduction

Checking if a string contains only digits is a common task in data validation and text processing. This operation verifies that every character in the string is a digit (0-9). This guide will demonstrate how to write a Go program that checks if a given string contains only digits.

Problem Statement

Create a Go program that:

  • Prompts the user to enter a string.
  • Checks if the string contains only digits.
  • Displays whether the string contains only digits or not.

Example:

  • Input: "12345"

  • Output: "The string contains only digits."

  • Input: "123a45"

  • Output: "The string does not contain only digits."

Solution Steps

  1. Import the fmt and unicode Packages: Use import "fmt" for formatted I/O operations and import "unicode" for character checks.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Input the String: Use fmt.Scanln or fmt.Scanf to take input from the user for the string.
  4. Check if the String Contains Only Digits: Use a loop and the unicode.IsDigit function to check if all characters in the string are digits.
  5. Display the Result: Use fmt.Println to display whether the string contains only digits.

Go Program

package main

import (
    "fmt"
    "unicode"
)

/**
 * Go Program to Check if a String Contains Only Digits
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the input string
    var input string

    // Step 2: Prompt the user to enter a string
    fmt.Print("Enter a string: ")
    fmt.Scanln(&input)

    // Step 3: Check if the string contains only digits
    isDigitOnly := true
    for _, char := range input {
        if !unicode.IsDigit(char) {
            isDigitOnly = false
            break
        }
    }

    // Step 4: Display the result
    if isDigitOnly {
        fmt.Println("The string contains only digits.")
    } else {
        fmt.Println("The string does not contain only digits.")
    }
}

Explanation

Step 1: Declare Variables

  • The variable input is declared to store the user’s input string.

Step 2: Input the String

  • The program prompts the user to enter a string using fmt.Print. The fmt.Scanln function reads the input and stores it in the input variable.

Step 3: Check if the String Contains Only Digits

  • The program initializes a boolean variable isDigitOnly to true.
  • It then iterates over each character in the string using a for loop.
  • The unicode.IsDigit function checks if each character is a digit. If any character is not a digit, isDigitOnly is set to false, and the loop breaks.

Step 4: Display the Result

  • The program checks the value of isDigitOnly and prints an appropriate message indicating whether the string contains only digits or not.

Output Example

Example 1:

Enter a string: 12345
The string contains only digits.

Example 2:

Enter a string: 123a45
The string does not contain only digits.

Conclusion

This Go program demonstrates how to check if a string contains only digits using the unicode.IsDigit function. It covers basic programming concepts such as string manipulation, loops, and conditional statements. This example is useful for beginners learning Go programming and understanding how to validate string content effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top