Below is the list of Character Functions with their usage in programming:
Classification Function | Definition |
---|---|
isalnum(ch) | This function returns true if the character ch is a letter or a digit. |
isalpha(ch) | This function returns true if the character ch is a letter. |
iscntrl(ch) | This function returns true if the character ch is a control character. (Ex: \t, \n, \b, \r) |
isdigit(ch) | This function returns true if the character ch is a digit. |
isgraph(ch) | This function returns true if the character ch is not a space but a printable value. |
islower(ch) | This function returns true if the character ch is a lowercase letter. |
isupper(ch) | This function returns true if the character ch is an uppercase letter. |
isprint(ch) | This function returns true if the character ch is a printable character. |
ispunct(ch) | This function returns true if the character ch is a punctuation character. (Ex: !, #, $, @) |
isspace(ch) | This function returns true if the character ch is whitespace. |
isblank(ch) | This function returns true if the character ch is blank. |
isxdigit(ch) | This function returns true if the character ch is a hexadecimal digit. |
Transformation Function | Definition |
tolower(ch) | This function is used to change character ch to lowercase if it is an uppercase letter otherwise leave it unchanged. |
toupper(ch) | This function is used to change character ch to uppercase if it is a lowercase letter otherwise leave it unchanged. |
These in-built character functions take a single character as a parameter and return a value that can either be a boolean type value if it is a Classification function or a character type value if it is a Transformation function.
Let's discuss each character function with C++ example code:
1. isalnum(): This function returns true if the character is an alphabet or a number else it will return false. It is known as alphanumeric and includes characters from a-z, A-Z, and 0-9.
Example code:
/*C++ program to check isalnum() function*/ #include<iostream> #include<cctype> using namespace std; int main(){ char ch[10] = "12Abc@"; //working of isalnum() function for(int i = 0; i < 6; i++){ if(isalnum(ch[i])) cout<<ch[i]<<" is an alphanumeric character."<<endl; else cout<<ch[i]<<" is not an alphanumeric character."<<endl; } return 0; }
1 is an alphanumeric character. 2 is an alphanumeric character. A is an alphanumeric character. b is an alphanumeric character. c is an alphanumeric character. @ is not an alphanumeric character.
2. isalpha(): This function returns true if the character is an alphabet else it returns false. It includes characters from a-z and A-Z.
Example code:
/*C++ program to check isalpha() function*/ #include<iostream> #include<cctype> using namespace std; int main(){ char ch[10] = "1Ab@"; //working of isalpha() function for(int i = 0; i < 4; i++){ if(isalpha(ch[i])) cout<<ch[i]<<" is an alphabet character."<<endl; else cout<<ch[i]<<" is not an alphabet character."<<endl; } return 0; }
1 is not an alphabet character. A is an alphabet character. b is an alphabet character. @ is not an alphabet character.
3. iscntrl(): This function returns true if the character is a control character like \t, \n, \b, \r, etc else returns false.
Example code:
// C++ program to check iscntrl() #include <cctype> #include <iostream> using namespace std; int main(){ char ch[5] = "\nA\t\b"; //working of iscntrl() function for (int i = 0; i < 4; i++) { if (iscntrl(ch[i])) cout<<"Character is a control Character"<<endl; else cout<<"Character is a not control Character"<<endl; } return 0; }
Character is a control Character Character is a not control Character Character is a control Character Character is a control Character
4. isdigit(): This function returns true if the character is a digit else it returns false. It includes all digits from 0 to 9.
Example code:
//C++ program to check isdigit() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[5] = "A123"; //working of isdigit() for(int i = 0; i < 4; i++){ if(isdigit(ch[i])) cout<<ch[i]<<" is a digit."<<endl; else cout<<ch[i]<<" is not a digit."<<endl; } return 0; }
A is not a digit. 1 is a digit. 2 is a digit. 3 is a digit.
5. isgraph(): This function returns true if the character is a graphical character else it returns false. Graphical characters include digits (0-9), alphabets (a-z and A-Z), and punctuation characters (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~).
Example code:
//C++ program to check isgraph() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[5] = "$\tA2"; //working of isgraph() for(int i = 0; i < 4; i++){ if(isgraph(ch[i])) cout<<ch[i]<<" is a graphical character."<<endl; else cout<<ch[i]<<" is not a graphical character."<<endl; } return 0; }
$ is a graphical character. is not a graphical character. A is a graphical character. 2 is a graphical character.
6. islower(): This function returns true if the character is a lowercase letter (a-z) else it returns false.
7. isupper(): This function returns true if the character is an uppercase letter (A-Z) else it returns false.
Example code:
//C++ program to check islower() and isupper() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[5] = "ABcd"; //working of isupper() and islower() for(int i = 0; i < 4; i++){ if(islower(ch[i])) cout<<ch[i]<<" is a lowercase character"<<endl; if(isupper(ch[i])) cout<<ch[i]<<" is an uppercase character"<<endl; } return 0; }
A is an uppercase character B is an uppercase character c is a lowercase character d is a lowercase character
8. isprint(): This function returns true if the character is a printable character else it returns false. All characters are printable character except the control characters.
Example code:
//C++ program to check isprint() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[6] = "A\tBc1"; //working of isprint() for(int i = 0; i < 5; i++){ if(isprint(ch[i])) cout<<ch[i]<<" is a printable character"<<endl; else cout<<ch[i]<<" is not a printable character"<<endl; } return 0; }
A is a printable character is not a printable character B is a printable character c is a printable character 1 is a printable character
9. ispunct(): This function returns true if the character is a punctuation character else it returns false.
Example code:
//C++ program to check ispunct() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[7] = ")#!\t2'"; //working of ispunct() for(int i = 0; i < 6; i++){ if(ispunct(ch[i])) cout<<ch[i]<<" is a punctuation character"<<endl; else cout<<ch[i]<<" is not a punctuation character"<<endl; } return 0; }
) is a punctuation character # is a punctuation character ! is a punctuation character is not a punctuation character 2 is not a punctuation character
10. isspace(): This function returns true if the character is a tab or a whitespace else it returns false.
Example code:
//C++ program to check isspace() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[7] = "A\n\t#2"; //working of isspace() for(int i = 0; i < 5; i++){ if(isspace(ch[i])) cout<<"Character is a space character"<<endl; else cout<<"Character is not a space character"<<endl; } return 0; }
Character is not a space character Character is a space character Character is a space character Character is not a space character Character is not a space character
11. isblank(): This function returns true if the character is a blank character like a space or a tab else it returns false.
Example code:
//C++ program to check isblank() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[7] = " \n\t2"; //working of isblank() for(int i = 0; i < 4; i++){ if(isblank(ch[i])) cout<<"Character is a blank character"<<endl; else cout<<"Character is not a blank character"<<endl; } return 0; }
Character is a blank character Character is not a blank character Character is a blank character Character is not a blank character
12. isxdigit(): This function returns true if the character is a hexadecimal character else it returns false. It includes characters from 0-9 and (A-F) in both uppercase and lowercase.
Example code:
//C++ program to check isxdigit() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch[7] = "AB120X"; //working of isxdigit() for(int i = 0; i < 6; i++){ if(isxdigit(ch[i])) cout<<ch[i]<<" is a Hexadecimal character"<<endl; else cout<<ch[i]<<" is not a Hexacecima character"<<endl; } return 0; }
A is a Hexadecimal character B is a Hexadecimal character 1 is a Hexadecimal character 2 is a Hexadecimal character 0 is a Hexadecimal character X is not a Hexacecima character
13. toupper(): This is a transformation function that change the character to uppercase letter if a lowercase character is pass to this function and if the character is already an uppercase character then no change take place.
14. tolower(): This is a transformation function that change the character to lowercase letter if an uppercase character is pass to this function and if the character is already a lowercase character then no change take place.
Example code:
//C++ program to check tolower() and toupper() function #include<iostream> #include<cctype> using namespace std; int main(){ char ch1[11] = "welcome"; char ch2[11] = "ALGOLESSON"; //converting to uppercase for(int i = 0; i < 7; i++){ cout<<(char)toupper(ch1[i]); } cout<<"\t"; //converting to lowercase for(int i = 0; i < 10; i++){ cout<<(char)tolower(ch2[i]); } return 0; }
WELCOME algolesson
So here ends the complete list of built-in character functions in C++ programming language with example code to make you understand working of each function. These functions are very helpful for solving several string related coding problems.
Next:
No comments:
Post a Comment