Check if a string is alphanumeric in C++

In this article, we will learn how to check whether a string is alphanumeric or not in C++.

Table Of Contents

Problem Description

Our C++ program takes the string as input from the user, and it will iterate through the characters of the string and check whether it contains only alphanumeric characters or not.

Input

string str = "thisPointer999"

Output

The string is alphanumeric.
  • If the given string contains only an alphanumeric character, then we have to print The string is alphanumeric.
  • If the given string contains any non-alphanumeric character, then we have to print The string is non-alphanumeric.

We can think of solving the given problem in four ways. Let’s understand all the approaches in details one by one.

Check if string is alphanumeric using std::count_if()

The first approach we will learn is the standard template library algorithm std::count_if() function. The count_if() function accepts a range and a callback function (predicate) as arguments. It applies the given function to all elements of the range. This range is the input iterators pointing to the starting and ending position of the string. The range used is [start, end), which contains all the elements between start and end, including the element pointed by start but not the element pointed by the end. The count_if() function returns the number of elements in the range [start,end) for which predicate is true.

We can pass all characters of string as range and isalnum() function, as arguments to the count_if() function. It will call the isalnum() function on each of the characters, to confirm if it is an alphanumeric character or not. So, the count_if() function counts the number of elements for which the isalnum() is true in the range[start,end). If the count value is equal to the length of the string, that means all the characters of the string are alphanumeric, and hence string is alphanumeric.

  • Time Complexity is of an order of n means linear. It is the range between first and last, comparing pairs of elements until a mismatch is identified. Time Complexity O(n).
  • Space Complexity is also of order 1 means constant. Space Complexity O(1).

Let’s see the practical example,

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string str = "thisPointer999";

    // Get count of alphanumeric characters in string
    int count = std::count_if(
                    std::begin(str),
                    std::end(str),
                    ::isalnum);

    // Check if count of alphanumeric characters in string
    // is equal to the length of string
    if(count == str.length()) 
    {
      std::cout << "The string is alphanumeric.\n";
    }
    else{
       std::cout << "The string is non-alphanumeric.\n";
    }

    return 0;
}

Output

The string is alphanumeric.

Check if string is alphanumeric using std::all_of()

The second approach we will learn is the standard template library algorithm std::all_of() function. It accepts a range and a callback function (predicate) as arguments. It applies the given function to all elements of the range. This range is the input iterators pointing to the starting and ending position of the string. The range used is [start, end), which contains all the elements between start and end, including the element pointed by start but not the element pointed by the end. If the function/predicate returns true for all the elements, then only all_of() function will return true. Otherwise, the all_of() will return false. Also, if the range is empty, then it also returns false.

We can pass all characters of string as range and isalnum() function as arguments to the all_of() function. It will call the 1 function on each of the characters to confirm if it is an alphanumeric character or not. If all_of() finds any character for which isalnum() returns false, it will return false. Otherwise, it will return true. If all_of() returns true, that means all the characters of the string are alphanumeric, and hence string is alphanumeric.

  • Time Complexity is of an order of n means linear. It is the range between first and last, comparing pairs of elements until a mismatch is identified. Time Complexity O(n).
  • Space Complexity is also of order 1 means constant. Space Complexity O(1).

Let’s see the practical example,

// Using std::all_of() to check whether a string is alphanumeric or not
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string str = "thisPointer199";

    // Iterating from beginning of the string to end of the string

    bool isalphaNumeric = all_of(begin(str), end(str),::isalnum);

    if(isalphaNumeric) 
    {
        cout << "The string is alphanumeric.\n";
    }
    else
    {
        cout << "The string is non-alphanumeric.\n";
    }
    return 0;
}

Output

The string is alphanumeric.

Check if string is alphanumeric using std::find_if_not()

The third approach we will learn is the standard template library algorithm std::find_if_not() function. The find_if_not() function accepts a range and a callback function (predicate) as arguments. It applies the given function to all elements of the range. This range is the input iterators pointing to the starting and ending position of the string. The range used is [start, end), which contains all the elements between start and end, including the element pointed by start but not the element pointed by the end. The find_if_not() function returns the iterator in the range [start,end) for which predicate is false.

We can pass all characters of string as range and isalnum() function as arguments to the find_if_not() function. We will call the isalnum() function on each of the characters to confirm if it is an alphanumeric character or not. So, the find_if_not() function return an iterator for which isalnum() is false in the range [start,end). If the iterator returned by the find_if_not() function equals the end of the string, that means all the characters of the string are alphanumeric, and hence string is alphanumeric.

  • Time Complexity is of an order of n means linear. It is the range between first and last, comparing pairs of elements until a mismatch is identified. Time Complexity O(n).
  • Space Complexity is also of order 1 means constant. Space Complexity O(1).

Let’s see the practical example,

// Using std::find_if_not() to check whether a string is alphanumeric or not
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string str = "thisPointer909";

    // Iterating from beginning of the string to end of the string
    bool isalphaNumeric = find_if_not(
                                begin(str),
                                end(str),
                                ::isalnum) == str.end();

    if(isalphaNumeric) 
    {
        cout << "The string is alphanumeric.\n";
    }
    else
    {
        cout << "The string is non-alphanumeric.\n";
    }

    return 0;
}

Output

The string is alphanumeric.

Check if string is alphanumeric using std::isalnum()

The fourth and last approach we will learn is the standard template library algorithm std::isalnum(). It takes one character at a time and checks whether it is alphanumeric or not. So we use this in our program to count the number of characters for which the isalnum() function is false. If the count is equal to or greater than 1, then it indicates that the string has some non-alphanumeric characters; hence, the given string is non-alphanumeric. Otherwise, if the count is equal to zero, it indiactes that the string has alphanumeric characters, and therefore the given string is alphanumeric.

  • Time Complexity is of an order of n means linear. It is the range between first and last, comparing pairs of elements until a mismatch is identified. Time Complexity O(n).
  • Space Complexity is also of order 1 means constant. Space Complexity O(1).

Code example,

// Using std::isalnum() to check whether a string is alphanumeric or not
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string str = "thisPointer999";

    // Iterating from beginning of the string to end of the string

    int count=0;
    for (const char& ch: str)
    {
        if (!isalnum(ch))
        {
            count++;
        }
    }

    if(count) 
    {
        cout << "The string is non-alphanumeric.\n";
    }
    else{
        cout << "The string is alphanumeric.\n";
    }

    return 0;
}

Output

The string is alphanumeric.

Summary

We have seen a detailed explanation of four approaches to checking whether the string is alphanumeric or not. The first one is using the std::count_if(), second is std::all_of(), third one is std::find_if_not() , and the last is using std::isalnum(). Also, we have seen the Time and Space Complexity of all four approaches. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top