Check input character is alphabet, digit or special character
Last Updated :
17 Feb, 2023
All characters whether alphabet, digit or special character have ASCII value. Input character from the user will determine if it’s Alphabet, Number or Special character.
ASCII value ranges-
- For capital alphabets 65 – 90
- For small alphabets 97 – 122
- For digits 48 – 57
Examples :
Input : 8
Output : Digit
Input : E
Output : Alphabet
C++
#include <iostream>
using namespace std;
void charCheck( char input_char)
{
if ((input_char >= 65 && input_char <= 90)
|| (input_char >= 97 && input_char <= 122))
cout << " Alphabet " ;
else if (input_char >= 48 && input_char <= 57)
cout << " Digit " ;
else
cout << " Special Character " ;
}
int main()
{
char input_char = '$' ;
charCheck(input_char);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void charCheck( char input_char)
{
if ((input_char >= 65 && input_char <= 90 )
|| (input_char >= 97 && input_char <= 122 ))
System.out.println( " Alphabet " );
else if (input_char >= 48 && input_char <= 57 )
System.out.println( " Digit " );
else
System.out.println( " Special Character " );
}
public static void main(String[] args)
{
char input_char = '$' ;
charCheck(input_char);
}
}
|
Python3
def charCheck(input_char):
if (( int ( ord (input_char)) > = 65 and
int ( ord (input_char)) < = 90 ) or
( int ( ord (input_char)) > = 97 and
int ( ord (input_char)) < = 122 )):
print ( " Alphabet " )
elif ( int ( ord (input_char)) > = 48 and
int ( ord (input_char)) < = 57 ):
print ( " Digit " )
else :
print ( " Special Character " )
input_char = '$'
charCheck(input_char)
|
C#
using System;
class GFG {
static void charCheck( char input_char)
{
if ((input_char >= 65 && input_char <= 90)
|| (input_char >= 97 && input_char <= 122))
Console.WriteLine( " Alphabet " );
else if (input_char >= 48 && input_char <= 57)
Console.WriteLine( " Digit " );
else
Console.WriteLine( "Special Character" );
}
public static void Main()
{
char input_char = '$' ;
charCheck(input_char);
}
}
|
PHP
<?php
function charCheck( $input_char )
{
if (( $input_char >= 65 && $input_char <= 90)
|| ( $input_char >= 97 && $input_char <= 122))
echo " Alphabet " ;
else if ( $input_char >= 48 &&
$input_char <= 57)
echo " Digit " ;
else
echo " Special Character " ;
}
$input_char = '$' ;
charCheck( $input_char );
?>
|
Javascript
<script>
function charCheck(input)
{
var input_char = input.charCodeAt(0);
if (
(input_char >= 65 && input_char <= 90) ||
(input_char >= 97 && input_char <= 122)
)
document.write( " Alphabet " );
else if (input_char >= 48 && input_char <= 57)
document.write( " Digit " );
else document.write( " Special Character " );
}
var input_char = "$" ;
charCheck(input_char);
</script>
|
Output :
Special Character
Time complexity: O(1) as constant operations are done
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...