Home | | Computer Science 11th std | Character functions (ctype.h) - C++ Header Files and Built-in Functions

Chapter: 11th Computer Science : Chapter 11 : Functions

Character functions (ctype.h) - C++ Header Files and Built-in Functions

This header file defines various operations on characters.

Character functions (ctype.h)

 

This header file defines various operations on characters. Following are the various character functions available in C++. The header file ctype.h is to be included to use these functions in a program.

 

1. isalnum()

 

This function is used to check whether a character is alphanumeric or not. This function returns non-zero value if c is a digit or a letter, else it returns 0.

 

Syntax:

int isalnum (char c)

 

Example :

int r = isalnum(‘5’);

cout << isalnum('A') <<’\t’<<r;

But the statements given below assign 0 to the variable n, since the given character is neither an alphabet nor a digit.

char c = '$';

int n = isalnum(c);

cout<<c;

Output:

0

 

Program 11.3

#include<iostream>

#include<stdio.h>

#include<ctype.h>

using namespace std;

int main()

{

      char ch;

      int r;

      cout<<"\n Type a Character :";

      ch = getchar();

      r = isalnum(ch);

      cout<<"\nThe Return Value of isalnum(ch) is :"<<r;

}

Output-1:

Type a Character :A

The Return Value of isalnum(ch) is :1

Output-2:

Type a Character :?

The Return Value of isalnum(ch) is :0

 

2. isalpha()

 

The isalpha() function is used to check whether the given character is an alphabet or not.

 

Syntax:

int isalpha(char c);

This function will return 1 if the given character is an alphabet, and 0 otherwise 0. The following statement assigns 0 to the variable n, since the given character is not an alphabet.

int n = isalpha(‘3’);

But, the statement given below displays 1, since the given character is an alphabet.

cout << isalpha('a');

 

Program 11.4

#include<iostream>

#include<stdio.h>

#include<ctype.h>

using namespace std;

int main()

{

      char ch;

      cout << "\n Enter a charater: ";

      ch = getchar();

      cout<<"\n The Return Value of isalpha(ch) is :" << isalpha(ch) ;

}

Output-1:

Enter a charater: A

The Return Value of isalpha(ch) is :1

Output-2:

Enter a charater: 7

The Return Value of isalpha(ch) is :0

 

3. isdigit()

 

This function is used to check whether a given character is a digit or not. This function will return 1 if the given character is a digit, and 0 otherwise.

 

Syntax:

int isdigit(char c);

When the following program is executed, the value of the variable n will be 1,since the given character is not a digit.

 

Program 11.5

using namespace std;

#include<iostream>

#include<ctype.h>

int main()

{

      char ch;

      cout << "\n Enter a Character: ";

      cin >> ch;

      cout<<"\n The Return Value of isdigit(ch) is :" << isdigit(ch) ;

}

Output-1

Enter a Character: 3

The Return Value of isdigit(ch) is :1

Output-2

Enter a Character: A

The Return Value of isdigit(ch) is :0

 

*Return 0; (Not Compulsory in latest compilers)


4. islower()

 

This function is used to check whether a character is in lower case (small letter) or not. This functions will return a non-zero value, if the given character is a lower case alphabet, and 0 otherwise.

 

Syntax:

int islower(char c);

After executing the following statements, the value of the variable n will be 1 since the given character is in lower case.

char ch = 'n';

int n = islower(ch);

But the statement given below will assign 0 to the variable n, since the given character is an uppercase alphabet.

int n = islower('P');

 

5. isupper()

 

This function is used to check the given character is uppercase. This function will return 1 if true otherwise 0. For the following examples value 1 will be assigned to n and 0 for m.

int n=isupper(‘A’);

int m=isupper(‘a’);

 

6. toupper()

 

This function is used to convert the given character into its uppercase. This function will return the upper case equivalent of the given character. If the given character itself is in upper case, the output will be the same.

 

Syntax:

char toupper(char c);

The following statement will assign the character constant 'K' to the variable c.

char c = toupper('k’);

But, the output of the statement given below will be 'B' itself.

cout <<toupper('B');


7. tolower()

 

This function is used to convert the given character into its lowercase. This function will return the lower case equivalent of the given character. If the given character itself is in lower case, the output will be the same.

 

Syntax:

char tolower(char c);

The following statement will assign the character constant 'k' to the variable c.

char c = tolower('K’);

But, the output of the statement given below will be 'b' itself.

cout <<tolower('b');

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 11 : Functions : Character functions (ctype.h) - C++ Header Files and Built-in Functions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.