BlogsDope image BlogsDope

Predefined functions for individual characters, ctype.h

July 5, 2017 EXAMPLE C FUNCTION 2279

This post contains a list of predefined functions defined in ctype.h which we can use on an individual C character. You can learn more about C string from our C course.

The functions present in the ctpye.h header are:

Function Use
isalnum checks if a character is an alphanumeric
isalpha checks if a character is an alphabet
iscntrl checks if a character is control character
isdigit checks if a character is digit
isgraph checks if a character is graphical representation
islower checks if a character is lowercase
isprint checks if a character is printable
ispunct checks if a character is punctuation
issapce checks if a charater is white-space
isupper checks if a charater is uppercase
isxdigit checks if a character is hexadecimal digit
tolower converts character to lowercase
toupper converts character to uppercase

isalnum

isalnum(x) will be true when x is an alphabet or a digit.

#include <stdio.h>
#include <ctype.h>

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

Please note that the parameter of the function ‘alnum’ is an int i.e., int alnum(int c) and it just typecasts other data types to int. For example, we passed a char in the above examples and then it was typecasted to int.

isalpha

int isalpha(int c)

isaplha(x) will be true when x is an alphabet.

#include <stdio.h>
#include <ctype.h>

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

iscntrl

int iscntrl(int c)

iscntrl(x) will be true when x is a control character.

A control charater is a character that doesn’t occupy a printing space. It means that it is a non-printing charater. Some of the control characters are ‘\0’, ‘\t’, ‘e’, etc.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char s[] = "Hello\nBlogsDope\n";
    int i = 0;
    while(s[i] != '\0')
    {
        if(iscntrl(s[i]))
            printf("%c is a control character\n",s[i]);
        else
            printf("%c is NOT a control character\n",s[i]);
        i++;
    }
    return 0;
}

H is NOT a control character
e is NOT a control character
l is NOT a control character
l is NOT a control character
o is NOT a control character

 is a control character
B is NOT a control character
l is NOT a control character
o is NOT a control character
g is NOT a control character
s is NOT a control character
D is NOT a control character
o is NOT a control character
p is NOT a control character
e is NOT a control character

 is a control character

isdigit

int isdigit(int c)

isdigit(x) will be true when x is a decimal digit.

#include <stdio.h>
#include <ctype.h>

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;
}

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

isgraph

int isgraph(int c)

isgraph(x) will be true when x has a graphical representation.

All printable characters have graphical representation except the space character (‘ ’).

#include <stdio.h>
#include <ctype.h>

int main()
{
    char s[] = "123abc!@ \n #";
    int i = 0;
    while(s[i] != '\0')
    {
        if(isgraph(s[i]))
            printf("%c has graphical representation\n",s[i]);
        i++;
    }
    return 0;
}

1 has graphical representation
2 has graphical representation
3 has graphical representation
a has graphical representation
b has graphical representation
c has graphical representation
! has graphical representation
@ has graphical representation
# has graphical representation

islower

int islower(int c)

islower(x) will be true when x is in the lowercase.

#include <stdio.h>
#include <ctype.h>

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;
}

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

isprint

int isprint(int c)

isprint(x) will be true when x is a printable character (opposite of ‘iscntrl’).

#include <stdio.h>
#include <ctype.h>

int main()
{
    char s[] = "Blogs\nDope";
    int i = 0;
    while(s[i] != '\0')
    {
        if(isprint(s[i]))
            printf("%c is printable\n",s[i]);
        else
            printf("%c is NOT printable\n",s[i]);
        i++;
    }
    return 0;
}

B is printable
l is printable
o is printable
g is printable
s is printable

 is NOT printable
D is printable
o is printable
p is printable
e is printable

ispunct

int ispunct(int c)

punct(x) will be true when x is a punctuation character.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char s[] = "Hello, BlogsDope!";
    int i = 0;
    while(s[i] != '\0')
    {
        if(ispunct(s[i]))
            printf("%c is a punctuation character\n",s[i]);
        else
            printf("%c is NOT a punctuation character\n",s[i]);
        i++;
    }
    return 0;
}

H is NOT a punctuation character
e is NOT a punctuation character
l is NOT a punctuation character
l is NOT a punctuation character
o is NOT a punctuation character
, is a punctuation character
  is NOT a punctuation character
B is NOT a punctuation character
l is NOT a punctuation character
o is NOT a punctuation character
g is NOT a punctuation character
s is NOT a punctuation character
D is NOT a punctuation character
o is NOT a punctuation character
p is NOT a punctuation character
e is NOT a punctuation character
! is a punctuation character

isspace

int isspace(int c)

isspace(x) will be true when x is a white-space character e.g.- ‘ ’, ‘\t’, ‘\n’, ‘\v’, etc.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char s[] = "Hello, BlogsDope!\n";
    int i = 0;
    while(s[i] != '\0')
    {
        if(isspace(s[i]))
            printf("%c is a white-space\n",s[i]);
        else
            printf("%c is NOT a white-space\n",s[i]);
        i++;
    }
    return 0;
}

H is NOT a white-space
e is NOT a white-space
l is NOT a white-space
l is NOT a white-space
o is NOT a white-space
, is NOT a white-space
  is a white-space
B is NOT a white-space
l is NOT a white-space
o is NOT a white-space
g is NOT a white-space
s is NOT a white-space
D is NOT a white-space
o is NOT a white-space
p is NOT a white-space
e is NOT a white-space
! is NOT a white-space

 is a white-space

isupper

int isupper(int c)

isupper(x) will be true if x is in the uppercase.

#include <stdio.h>
#include <ctype.h>

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;
}

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

isxdigit

int isxdigit(int c)

isxdigit(x) will be true when x is a hexadecimal digit.

Any digits of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E and F are hexadeciaml digits.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char s[] = "asd123!@#";
    int i = 0;
    while(s[i] != '\0')
    {
        if(isxdigit(s[i]))
            printf("%c is a hexadecimal\n",s[i]);
        else
            printf("%c is NOT a hexadecimal\n",s[i]);
        i++;
    }
    return 0;
}

a is a hexadecimal
s is NOT a hexadecimal
d is a hexadecimal
1 is a hexadecimal
2 is a hexadecimal
3 is a hexadecimal
! is NOT a hexadecimal
@ is NOT a hexadecimal
# is NOT a hexadecimal

tolower

int tolower(int c)

tolower(x) converts x into lowercase.

#include <stdio.h>
#include <ctype.h>

int main()
{
    printf("%c\n",tolower('A'));
    return 0;
}

a

toupper

int toupper(int c)

toupper(x) converts x into uppercase.

#include <stdio.h>
#include <ctype.h>

int main()
{
    printf("%c\n",toupper('a'));
    return 0;
}

A


Liked the post?
Developer and founder of CodesDope.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).