Menu Close

Go – Program to check alphabetic characters in strings in go

Here, we will write a program to check alphabetic characters in strings in go. Given below, you can find algorithm and program.

I have written program to check alphabetic characters in strings in go using regular expression. Regex make life easier :p

Program to check if all the characters are alphabetic in string.

Input: HelloGo
Ouput: true

Input: Hello12go
Ouput: false

Input: hello go
Output: false

Code:

package main

import (

 "fmt"

)

func check_alphabet_char_in_str(str string) bool {

 for _, char := range str {

   if ((char < 'a' || char > 'z') &&
       (char < 'A' || char > 'Z')) {

     return false

   }
 }
 return true
}

func main() {

  res := check_alphabet_char_in_str("HelloGo")

  fmt.Println(res)


  res = check_alphabet_char_in_str("Hello12go")

  fmt.Println(res)


  res = check_alphabet_char_in_str("1234")

  fmt.Println(res)

}

Output:

true

false

false

Using regular expression:

package main

import (

 "fmt"
 "regexp"

)

func main() {

  reg_fun := regexp.MustCompile(`^[a-zA-Z]+$`).MatchString

  res := reg_fun("HelloGo")

  fmt.Println(res)


  res = reg_fun("Hello 12 Go")

  fmt.Println(res)


  res = reg_fun("1234")

  fmt.Println(res)


  res = reg_fun("Hello12Go")

  fmt.Println(res)

}

Output:

true

false

false

false

Program to check if any character is alphabetic in string.

Input: Hello Go
Ouput: true

Input: Hello 12 go
Ouput: true

Input: 1234
Output: false

Code:

package main

import (

 "fmt"

)

func check_alphabet_char_in_str(str string) bool {

 for _, char := range str {

   if ((char >= 'a' && char <= 'z') ||
       (char >= 'A' && char <= 'Z')) {

     return true

   }
 }
 return false
}

func main() {

  res := check_alphabet_char_in_str("Hello Go")

  fmt.Println(res)


  res = check_alphabet_char_in_str("Hello 12 go")

  fmt.Println(res)


  res = check_alphabet_char_in_str("1234")

  fmt.Println(res)

}

Output:

true

true

false

Program to check alphanumeric character in string.

Code:

package main

import (

 "fmt"

)

func is_str_alpha_numeric(str string) bool {

  for _, char := range str {

    if ((char >= 'a' && char <= 'z') ||
        (char >= 'A' && char <= 'Z') ||
        (char >= '0' && char <= '9')) {

      return true

    }
  }
  return false
}

func main() {

  res := is_str_alpha_numeric("HelloGo")

  fmt.Println(res)


  res = is_str_alpha_numeric("Hello 12 Go")

  fmt.Println(res)


  res = is_str_alpha_numeric("@#$%")

  fmt.Println(res)


  res = is_str_alpha_numeric("Hello#@Go")

  fmt.Println(res)


}

Output:

true

true

false

true

Using regular expression:

package main

import (

 "fmt"
 "regexp"

)

func main() {

  reg_fun := regexp.MustCompile(`^[a-zA-Z0-9_]*$`).MatchString

  res := reg_fun("HelloGo")

  fmt.Println(res)


  res = reg_fun("Hello 12 Go")

  fmt.Println(res)


  res = reg_fun("1234")

  fmt.Println(res)


  res = reg_fun("Hello12Go")

  fmt.Println(res)

  res = reg_fun("Hello#@Go")

  fmt.Println(res)


}

Output:

true

false

true

true

false

To learn more about golang. Please follow given below link.

https://techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/

Posted in golang, golang program

Leave a Reply

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