给定一个字符,任务是检查给定字符是大写,小写还是非字母字符
例子:
Input: ch = 'A'
Output: A is an UpperCase character
Input: ch = 'a'
Output: a is an LowerCase character
Input: ch = '0'
Output: 0 is not an aplhabetic character
方法:解决此问题的关键在于字符的ASCII值。这是找出字符的最简单方法。通过以下详细信息可以解决此问题:
- 大写字母(AZ)在ASCII值的65-91范围内
- 小写字母(az)在ASCII值的97-122范围内
- 其他任何ASCII值都是非字母字符。
执行:
C++
// C++ implementation of the above approach
#include
using namespace std;
void check(char ch)
{
if (ch >= 'A' && ch <= 'Z')
cout<< ch << " is an UpperCase character\n";
else if (ch >= 'a' && ch <= 'z')
cout<< ch << " is an LowerCase character\n";
else
cout<< ch << " is not an aplhabetic character\n";
}
// Driver Code
int main()
{
char ch;
// Get the character
ch = 'A';
// Check the character
check(ch);
// Get the character
ch = 'a';
// Check the character
check(ch);
// Get the character
ch = '0';
// Check the character
check(ch);
return 0;
}
// This code is contributed by Code_Mech
C
// C implementation of the above approach
#include
void check(char ch)
{
if (ch >= 'A' && ch <= 'Z')
printf("\n%c is an UpperCase character",
ch);
else if (ch >= 'a' && ch <= 'z')
printf("\n%c is an LowerCase character",
ch);
else
printf("\n%c is not an aplhabetic character",
ch);
}
// Driver Code
int main()
{
char ch;
// Get the character
ch = 'A';
// Check the character
check(ch);
// Get the character
ch = 'a';
// Check the character
check(ch);
// Get the character
ch = '0';
// Check the character
check(ch);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
static void check(char ch)
{
if (ch >= 'A' && ch <= 'Z')
System.out.println("\n" + ch +
" is an UpperCase character");
else if (ch >= 'a' && ch <= 'z')
System.out.println("\n" + ch +
" is an LowerCase character" );
else
System.out.println("\n" + ch +
" is not an aplhabetic character" );
}
// Driver Code
public static void main(String []args)
{
char ch;
// Get the character
ch = 'A';
// Check the character
check(ch);
// Get the character
ch = 'a';
// Check the character
check(ch);
// Get the character
ch = '0';
// Check the character
check(ch);
}
}
// This code is contributed by Ryuga
Python3
# Python3 implementation of the above approach
def check(ch):
if (ch >= 'A' and ch <= 'Z'):
print(ch,"is an UpperCase character");
elif (ch >= 'a' and ch <= 'z'):
print(ch,"is an LowerCase character");
else:
print(ch,"is not an aplhabetic character");
# Driver Code
# Get the character
ch = 'A';
# Check the character
check(ch);
# Get the character
ch = 'a';
# Check the character
check(ch);
# Get the character
ch = '0';
# Check the character
check(ch);
# This code is contributed by mits
C#
// C# implementation of the above approach
using System;
class GFG
{
static void check(char ch)
{
if (ch >= 'A' && ch <= 'Z')
Console.WriteLine("\n" + ch +
" is an UpperCase character");
else if (ch >= 'a' && ch <= 'z')
Console.WriteLine("\n" + ch +
" is an LowerCase character" );
else
Console.WriteLine("\n" + ch +
" is not an aplhabetic character" );
}
// Driver Code
public static void Main(String []args)
{
char ch;
// Get the character
ch = 'A';
// Check the character
check(ch);
// Get the character
ch = 'a';
// Check the character
check(ch);
// Get the character
ch = '0';
// Check the character
check(ch);
}
}
// This code is contributed by Rajput-JI
PHP
= 'A' && $ch <= 'Z')
print($ch . " is an UpperCase character\n");
else if ($ch >= 'a' && $ch <= 'z')
print($ch . " is an LowerCase character\n");
else
print($ch . " is not an aplhabetic " .
"character\n");
}
// Driver Code
// Get the character
$ch = 'A';
// Check the character
check($ch);
// Get the character
$ch = 'a';
// Check the character
check($ch);
// Get the character
$ch = '0';
// Check the character
check($ch);
// This code is contributed by mits
?>
Javascript
输出:
A is an UpperCase character
a is an LowerCase character
0 is not an aplhabetic character
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。