The C++ character functions are basically the functions that take only a single character in the form of a parameter (casted to int) and then return a result. Moreover, a list is available below of the predefined functions defined in the ‘ctype.h’. Thus, the character functions that are available in the ‘ctpye.h’ header are as follows:
isalnum
isalnum(x) will be true when ‘x’ will be an alphabet or a digit.
#include
#include
int main()
{
char s[] = "123abc!@#";
int i = 0;
while(s[i] != '\0')
{
if(isalnum(s[i]))
printf("%c is alphanumeric\n",s[i]);
else
printf("%c is NOT alphanumeric\n",s[i]);
i++;
}
return 0;
}
Output
1 is alphanumeric
2 is alphanumeric
3 is alphanumeric
a is alphanumeric
b is alphanumeric
c is alphanumeric
! is NOT alphanumeric
@ is NOT alphanumeric
# is NOT alphanumeric
isalpha
int isalpha(int c)
isaplha(x) will be true when ‘x’ will be an alphabet.
#include
#include
int main()
{
char s[] = "123abc!@#";
int i = 0;
while(s[i] != '\0')
{
if(isalpha(s[i]))
printf("%c is an alphabet\n",s[i]);
else
printf("%c is NOT an alphabet\n",s[i]);
i++;
}
return 0;
}
Output
1 is NOT an alphabet
2 is NOT an alphabet
3 is NOT an alphabet
a is an alphabet
b is an alphabet
c is an alphabet
! is NOT an alphabet
@ is NOT an alphabet
# is NOT an alphabet
Browse more Topics under Inbuilt Functions
isdigit
int isdigit(int c)
isdigit(x) will be true when ‘x’ will be a decimal digit.
#include
#include
int main()
{
char s[] = "123abc!@#";
int i = 0;
while(s[i] != '\0')
{
if(isdigit(s[i]))
printf("%c is a digit\n",s[i]);
else
printf("%c is NOT a digit\n",s[i]);
i++;
}
return 0;
}
Output
1 is a digit
2 is a digit
3 is a digit
a is NOT a digit
b is NOT a digit
c is NOT a digit
! is NOT a digit
@ is NOT a digit
# is NOT a digit
islower
int islower(int c)
islower(x) will be true when ‘x’ will be in the lowercase.
#include
#include
int main()
{
char s[] = "BlogsDope";
int i = 0;
while(s[i] != '\0')
{
if(islower(s[i]))
printf("%c is in lowercase\n",s[i]);
else
printf("%c is NOT in lowercase \n",s[i]);
i++;
}
return 0;
}
Output
B is NOT in lowercase
l is in lowercase
o is in lowercase
g is in lowercase
s is in lowercase
D is NOT in lowercase
o is in lowercase
p is in lowercase
e is in lowercase
isupper
int isupper(int c)
isupper(x) will be true if ‘x’ will be in the uppercase.
#include
#include
int main()
{
char s[] = "BlogsDope";
int i = 0;
while(s[i] != '\0')
{
if(isupper(s[i]))
printf("%c is in uppercase\n",s[i]);
else
printf("%c is NOT in uppercase\n",s[i]);
i++;
}
return 0;
}
Output
B is in uppercase
l is NOT in uppercase
o is NOT in uppercase
g is NOT in uppercase
s is NOT in uppercase
D is in uppercase
o is NOT in uppercase
p is NOT in uppercase
e is NOT in uppercase
tolower
int tolower(int c)
tolower(x) converts ‘x’ to the lowercase.
#include
#include
int main()
{
printf("%c\n",tolower('A'));
return 0;
}
a
toupper
int toupper(int c)
toupper(x) converts ‘x’ to the uppercase.
#include
#include
int main()
{
printf("%c\n",toupper('a'));
return 0;
}
A
FAQs on Character Functions
Question 1: Define, isalnum function.
Answer: int isalnum ( int c ); basically checks if the character is alphanumeric or not.
Question 2: Define, tolower function.
Answer: int tolower ( int c ); basically converts the uppercase letter to the lowercase letter.
Leave a Reply