Advertisement

C++ Program to check string is alphanumeric or not


In this program, You will learn how to check string is alphanumeric or not in C++.


Some list of alphanumeric string: xiith123, john34, amit23, abc_98

alphanumeric string is called with integer value.

Example: How to check string is alphanumeric or not in C++

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

int main() {
    char str[100];
    cout << "Enter a string:";
    cin.getline(str, 100);

    int i = 0, k = 0, dig = 0, p;
    i=strlen(str);

    for (int j = 0; j < i; j++) {

        p = str[j];
        if (p >= 97 && p <= 122)
            k++;
        else if (p >= 65 && p <= 90)
            k++;
        else if (p == 32)
            k++;
        else if (p >= 48 && p <= 57)
            dig++;
    }

    if (k > 0 && dig > 0) {
        cout << "String is alphanumeric:" << str;
    } else {
        cout << "String is not alphanumeric:" << str;
    }

    return 0;
}

Output:

Enter a string:Xiith34  
String is alphanumeric:Xiith34