Header Ads Widget

C++ program to check whether a character is alphabet or not

 

C++ program to check whether a character is alphabet or not

Given a character write a program to  check whether it is alphabet or not . For checking this we have three methods .

Method 1:- By using isalpha() function

Method 2:- By using if-else (character>='A' && character<='Z)

Method 3:- By using Ascii values . (Ascii value of A to Z is 65 to 90 and for a to z is 97 to 122)

Sample input:

5

Sample output:

5 is not a Alphabet

C++ program to check whether a character is alphabet or not

The objective of the code is to check whether it is a alphabet or not .

Method 1: (By using isalpha() function)

isalpha(character) :- it will take a argument and return True if it is a character otherwise return false . 

Code:-

#include <bits/stdc++.h>
using namespace std;

int main()
{
   char ch;
int a;
cin>>ch;
if(isalpha(ch))
cout<<ch<<" is a Alphabet";
else
cout<<ch<<" is not a Alphabet";
return 0;
}


Output:

z
z is a Alphabet



Method 2: 

#include <bits/stdc++.h>
using namespace std;

int main()
{
   char ch;
int a;
cin>>ch;
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
cout<<ch<<" is a Alphabet";
else
cout<<ch<<" is not a Alphabet";
return 0;
}

Output:

z
z is a Alphabet



Method 3: (By using Ascii values )

#include <bits/stdc++.h>
using namespace std;

int main()
{
   char ch;
int a;
cin>>ch;
// changing character into integer
// (i.e char into ascii)
a=ch;
if((a>=65 && a<=90) || (a>=97 && a<=122))
cout<<ch<<" is a Alphabet";
else
cout<<ch<<" is not a Alphabet";
return 0;
}


Output:-

z
z is a Alphabet



Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-