C++ program to check if a string contains only alphanumeric characters:
In this tutorial, we will learn how to check if all characters of a string are alphanumeric or not in C++
. Our program will ask the user to enter one string. It will iterate over the characters of the string one by one and print one message if the characters of the string are alphanumeric or not.
The cctype
header provides a function, isalnum
, to check for alphanumeric characters. The program will use this function to check if a character is alphanumeric or not.
Syntax of isalnum
:
The isalnum
function is defined as below:
int isalnum(int c);
Parameters and return values:
The isalnum
function takes one character as its parameter. It converts the character to an integer and checks if it is alphanumeric or not. The return value of this function is an integer. It returns zero for false`` and any other integer for
true`.
Method 1: C++ program to check if all characters of a string are alphanumeric by using a loop:
The following C++
program takes one string as input from the user and checks if it contains only alphanumeric characters or not:
#include <iostream>
using namespace std;
int main()
{
// 1
char str[100];
bool isAllAlphanumeric = true;
// 2
cout << "Enter a string to test:" << endl;
cin.get(str, 100);
// 3
for (int i = 0; str[i] != '\0'; i++)
{
// 4
if (!isalnum(str[i]))
{
isAllAlphanumeric = false;
break;
}
}
// 5
if (isAllAlphanumeric)
{
cout << "The string contains only alphanumeric characters" << endl;
}
else
{
cout << "The string doesn't contain only alphanumeric character" << endl;
}
}
- Download it on Github
Explanation :
The commented numbers in the above program denote the step numbers below :
-
Create one character array
str
of size 100 to assign the user-input string. The boolean variableisAllAlphanumeric
is initialized to define if all characters of the string are alphanumeric or not. -
It takes the user input string and assigns it to the
str
array variable. -
By using one
for
loop, it iterates over the characters of the string one by one. -
For each character, it uses the
isalnum()
function to check if the character is alphanumeric or not. If it is not alphanumeric, it assignsfalse
to theisAllAlphanumeric
variable and exits from the loop. -
At the end of the program, it prints one message based on the current value of
isAllAlphanumeric
.
Sample Output :
If you run this program, it will print outputs as below:
Enter a string to test:
hello world*
The string doesn't contain only alphanumeric character
Enter a string to test:
hell*o
The string doesn't contain only alphanumeric character
Enter a string to test:
hello
The string contains only alphanumeric characters
Method 2: Using find_if
:
The find_if
function is defined in the algorithm
library. We need to pass the start, end, and one predicate function as its arguments. It applies the predicate function to all the elements defined in the range [start, end)
i.e. it includes the element pointed by start and stops just before the element pointed by end. It will return the iterator for which the predicate returns true
.
#include <iostream>
#include <algorithm>
using namespace std;
bool isNotAlphaNumeric(char c)
{
return !isalnum(c);
}
int main()
{
string str;
cout << "Enter a string to test:" << endl;
getline(cin, str);
bool isAllAlphanumeric = find_if(begin(str), end(str), ::isNotAlphaNumeric) == str.end();
if (isAllAlphanumeric)
{
cout << "The string contains only alphanumeric characters" << endl;
}
else
{
cout << "The string doesn't contain only alphanumeric character" << endl;
}
}
-
Download it on Github
-
We are passing the function
isNotAlphaNumeric
as the predicate. It will stop if for any element it returnstrue
i.e. if any element is found that is not alphanumeric. -
It compares the result iterator to the end of the string. If it is equal, it means the characters of the string are alphanumeric. This value is assigned to the variable
isAllAlphanumeric
.
It will print similar output.
Method 3: By using find_if_not
:
The find_if_not
function is similar to the find_if
function. The only difference is that it stops if it finds any element for which the predicate returns false
. The parameters and return value of this function are similar to find_if
. We can pass the isalnum()
function as its predicate.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str;
cout << "Enter a string to test:" << endl;
getline(cin, str);
bool isAllAlphanumeric = find_if(begin(str), end(str), ::isalnum) == str.end();
if (isAllAlphanumeric)
{
cout << "The string contains only alphanumeric characters" << endl;
}
else
{
cout << "The string doesn't contain only alphanumeric character" << endl;
}
}
- Download it on Github
It will print similar results.
Method 4: By using all_of
:
The all_of
function also takes similar arguments. It takes one predicate as the third argument. It uses the predicate on all elements in that range and if it returns true
for all the elements, the all_of
function will return true
. Else, it will return false
. If the provided range is empty, it will return false
.
The following example shows how to use the all_of
function to check if a string holds only alphanumeric characters:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str;
cout << "Enter a string to test:" << endl;
getline(cin, str);
bool isAllAlphanumeric = all_of(begin(str), end(str), ::isalnum);
if (isAllAlphanumeric)
{
cout << "The string contains only alphanumeric characters" << endl;
}
else
{
cout << "The string doesn't contain only alphanumeric character" << endl;
}
}
- Download it on Github
The return value of all_of
is assigned to the variable isAllAlphanumeric
and it prints a message based on it.
Conclusion:
The isalnum()
function makes it easy to check if a character is alphanumeric or not. This function is also defined in the header <local>
, which is a local-specific version.
You might also like:
- C++ program to check if a character is a punctuation using ispunct
- C++ program to check if a character is a hexadecimal using isxdigit
- C++ check if a character is alphabetic using isalpha
- C++ program to pass a structure to a function
- How to run a C++ program in XCode in Mac
- C++ program to convert a decimal value to hexadecimal