Introduction

8 Most Useful C++ Functions: Character Functions article is about testing and conversion of characters using functions. What library should use? And how do character functions test various properties?

What is a function?

What is a C++function

A function is a self-contained union of code that performs a specific task. A function can be considered an independent, modular unit of code that can be reused in other parts of the program.

Functions are typically used to break down complex tasks into manageable pieces. This is especially true when the same task needs to be performed multiple times in the code.

Functions also allow programmers to make changes without affecting other parts of the program that call the same function.

Functions are sometimes called procedures, methods, or subroutines, depending on the programming language being used.

C++ Functions: Character Functions

Character functions are a way to represent the ASCII character with a function.The ASCII table is an international standard that assigns each letter and number a unique 8-bit code. The ASCII table represents text in computers, communication systems, and storage devices.Character functions provide a way for programmers to represent the ASCII character with a function. For example, the lowercase “a” can be represented by char_a(97) or char_A(65).

Standard library <cctype>

The C++ standard library provides several functions for examining the properties of and converting between different types of characters.The library provides functions for comparing characters, converting them to or from numeric values, and formatting them as strings. The header code: include <cctype> deals with C++ character functions.

C++ Functions: testing character

Character functions are used to classify strings as a single character or a set of characters.The following is a list of the different character functions:

  • isalpha() : checks whether the string contains only alphabetic characters.
  • isdigit() : checks whether the string contains only a digital.
  • islower() : checks whether the string contains only a lowercase letter.
  • islupper() : checks whether the string contains only an uppercase letter.
  • isalnum() : checks whether the string contains only a letter or a digital.
  • isspace() : checks whether the string contains only a whitespace character.
  • isprint() : checks whether the string contains only a printable character.
  • ispunct() : checks whether the string contains only a punctuation character.

C++ Functions: The character Functions examples

If the function returns 0, the result is true, and if the function returns one, the result is false.

Checks whether the string contains only alphabetic characters

C++ Functions isalpha

The code of the program

#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only alphabetic characters.int check = isalpha(‘T’);cout << check<<endl;return 0;}

The output of the program

The function returns 1, and the result is true.

Checks whether the string contains only a digital

C++ Functions isdigit

The code of the program

#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only a digital.int check = isdigit(‘2’);cout << check<<endl;return 0;}

The output of the program

The function returns 1, and the result is true.

Check whether the string contains only a lowercase letter.

C++ Functions islower

The code of the program

The code of the program#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only a digital.int check = islower(‘F’);cout << check<<endl;return 0;}

The output of the program

The function returns 0, and the result is false.

Check whether the string contains only an uppercase letter.

C++ Functions  isupper

The code of the program

#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only an uppercase letter.int check = isupper(‘G’);cout << check<<endl;return 0;}

The output of the program

The function returns 1, and the result is true.

Check whether the string contains only a letter or a digital one.

C++ Functions isalnum

The code of the program

#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only a letter or a digital.int check = isalnum(‘b’);cout << check<<endl;return 0;}

The output of the program

The function returns 2, and the result is true.

Check whether the string contains only a whitespace character.

C++ Functions isspace

The code of the program

#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only a whitespace character.int check = isspace(‘ ‘);cout << check<<endl;return 0;}

The output of the program

The function returns 8, and the result is true.

Check whether the string contains only a printable character.

C++ Functions isprint

The code of the program

#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only a printable character.int check = isprint(‘h’);cout << check<<endl;return 0;}

The output of the program

The function returns 2, and the result is true.

Check whether the string contains only a punctuation character.

C++ Functions  ispunct

The code of the program

#include <iostream>#include <cctype>using namespace std;int main() {// Check whether the string contains only a punctuation character.int check = ispunct(‘!’);cout << check<<endl;return 0;}

The output of the program

The function returns 16, and the result is true.

Conclusion

C++ functions are blocks of code that can be reused to perform a task. Functions allow programmers to break complex programs into more specific and manageable parts.

Functions are three types:

 Built-in FunctionsUser-defined FunctionsFunction Objects

In the blog, we explained all about C++ character functions and their main ain in the code block: testing. The C++ character functions we saw are isalpha(), isdigit(), islower(), islupper(), isalnum(), isspace(), isprint(), and ispunct(). We used a detailed explanation supported by simplified examples.

Pin It on Pinterest

Share This