Broad Network


C++ Character Handling

String in C++ Standard Library Simplified - Part 5

Forward: In this article, I explain how to handle characters in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 5 of my series, String in C++ Standard Library, Simplified. In this article, I explain how to handle characters in C++.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The C++ cctype Header
You need the cctype header file to manipulate characters in C++. You do not need the string class and its header file for the features of this part of the series. In this part, functions (not methods) of the cctype header file are used.

The isalnum Function
The syntax is,

        int isalnum(int c);

The return value is Boolean, which is either a positive non-zero value for true or zero for false. True is returned if the character is an alphabet or a digit. Try the following code.

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isalnum('F');
        
        return 0;
    }

You would normally use this function in a conditional. Try the following code:

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        if (isalnum('7'))
            {
                cout << "This is a number or an alphabetic character.";
         }
        
        return 0;
    }

You can use an identifier in place of the character. Try the following code:

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        char myChar = 'F';
        cout << isalnum(myChar);
        
        return 0;
    }

All the functions except the last two in this article behave in a similar way.

The isalpha Function
The syntax is:

    int isalpha(int c);

Checks if the character is an alphabet. Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isalpha('A');
        
        return 0;
    }

The isdigit Function
The syntax is:

    int isdigit(int c);

Checks if the character is a digit. Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isdigit('7');
        
        return 0;
    }

Note that the character (digit) is in single quotes.

The iscntrl function
The syntax is:

    int iscntrl(int c);

Checks whether a character is a control character. Try the following (ignore the warning for now),

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << iscntrl('^C');
        
        return 0;
    }

The isgraph Function
The syntax is,

    int isgraph(int c);

Test for any printable character, except space (' '). Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isgraph('b');
        
        return 0;
    }

The isprint Function
The syntax is,

        int isprint(int c);

Test if any character is printable including space. The isgraph does the same thing but excluding space (' '). Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isprint(' ');
        
        return 0;
    }

The ispunct Function
The syntax is:

    int ispunct(int c);

The function tests for any printing character, which is one of a locale-specific set of punctuation characters for which neither isspace nor isalnum is true. Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << ispunct(',');
        
        return 0;
    }

The isspace Function
The syntax is:

    int isspace(int c);

This test for any standard white space character (e.g. t) or any local-specific character for which isalnum (see above) is false. The following are standard white space characters: space (' '), form feed ('f'), new-line ('n'), carriage return ('r'), horizontal tab ('t'), and vertical tab ('v'). Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isspace('t');
        
        return 0;
    }

The islower function
The syntax is,

    int islower(int c);

Checks if a character is in lowercase. Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << islower('g');
        
        return 0;
    }

The isupper Function
The syntax is:

    int isupper(int c);

Checks if a character is in uppercase. Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isupper('H');
        
        return 0;
    }

The isxdigit Function
The syntax is:

    int isxdigit(int c);

Tests if character is hexadecimal digit. Try

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << isxdigit('x33');
        
        return 0;
    }

The tolower Function
The syntax is:

    int tolower(int c);

Converts an uppercase alphabet to a lowercase. If the function is successful, the converted character is returned in ASCII code. You will need another function to change the ASCII code to the converted lowercase. Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << tolower('H');
        
        return 0;
    }

The toupper Function
The syntax is:

        int toupper(int c);

Converts a lowercase alphabet to an uppercase. If the function is successful, the converted character is returned in ASCII code. You will need another function to change the ASCII code to the converted uppercase. Try,

#include <iostream>
#include <cctype>
using namespace std;

int main()
    {
        cout << toupper('h');
        
        return 0;
    }

That is it, for the cctype header file and character handling in C++. See you in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message