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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.