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
- Import the fmt and unicode Packages: Use
import "fmt"for formatted I/O operations andimport "unicode"for character checks. - Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Input the String: Use
fmt.Scanlnorfmt.Scanfto take input from the user for the string. - Check if the String Contains Only Digits: Use a loop and the
unicode.IsDigitfunction to check if all characters in the string are digits. - Display the Result: Use
fmt.Printlnto 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
inputis 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. Thefmt.Scanlnfunction reads the input and stores it in theinputvariable.
Step 3: Check if the String Contains Only Digits
- The program initializes a boolean variable
isDigitOnlytotrue. - It then iterates over each character in the string using a
forloop. - The
unicode.IsDigitfunction checks if each character is a digit. If any character is not a digit,isDigitOnlyis set tofalse, and the loop breaks.
Step 4: Display the Result
- The program checks the value of
isDigitOnlyand 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.