Check Type of String

Check Type of String” is a problem to check whether the given string of length ‘n’ is alphabetic or numeric or alphanumeric.

Alphabetic String: An alphabetic string is a type of string which contains only alphabets.

Numeric String: A numeric string is a type of string which contains only numbers.

Alphanumeric String: An alphanumeric string is a type of string which contains both alphabets and numbers.


Example (Check Type of String):

INPUT:
dffgdassAA
OUTPUT:
dffgdassAA is an alphabetic string
 
INPUT:
Sdfjhd23sd1
OUTPUT:
Sdfjhd23sd1 is an alphanumeric string
 
INPUT:
123434332
OUTPUT:
123434332 is a numeric string.

The steps required to check type of string; checking whether string is alphabetic or numeric or alphanumeric is as follows:

  1. Initialize two flag variables, namely, alphabetic and numeric with 0.
  2. Scan the complete string and check whether encountered character is alphabet or number. 
  3. If encountered character is alphabet, set flag variable alphabetic with 1, else if encountered character is number, set flag variable numeric with 1.
  4. If alphabetic is set to 1 and numeric is set to 0, then given string is alphabetic string.
  5. If alphabetic is set to 0 and numeric is set to 1, then given string is numeric string.
  6. If alphabetic is set to 1 and numeric is set to 1, then given string is alphanumeric string.

C++ Program to check type of string, check whether string is alphabetic or numeric or alphanumeric is as follows:

/* Program to check type of string */
#include<bits/stdc++.h> 
#define len 1000 
using namespace std;  
int main()  
{  
    char str[len];  
    cout<<"Enter the string:\n";  
    cin>>str;  
    int alphabetic = 0;  
    int numeric = 0;  
    for(int i = 0; i < 1000 && str[i]!='
 /* Program to check type of string */ #include<bits/stdc++.h> #define len 1000 using namespace std; int main() { char str[len]; cout<<"Enter the string:\n"; cin>>str; int alphabetic = 0; int numeric = 0; for(int i = 0; i < 1000 && str[i]!='\0'; i++) { if(str[i]>='0' && str[i]<='9') numeric = 1; else if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) alphabetic = 1; } if(numeric == 0 && alphabetic == 1) cout<<str<<" is an alphabetic string"; else if(numeric == 1 && alphabetic == 0) cout<<str<<" is a numeric string"; else if(numeric == 1 && alphabetic == 1) cout<<str<<" is an alphanumeric string"; } 
'; i++) { if(str[i]>='0' && str[i]<='9') numeric = 1; else if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) alphabetic = 1; } if(numeric == 0 && alphabetic == 1) cout<<str<<" is an alphabetic string"; else if(numeric == 1 && alphabetic == 0) cout<<str<<" is a numeric string"; else if(numeric == 1 && alphabetic == 1) cout<<str<<" is an alphanumeric string"; }
OUTPUT:
Enter the string:123 
123 is a numeric string
Time Complexity:
O(n), where n is a length of string.

Related Posts:

  1. Program to copy one String to Another.
  2. Program to Concatenate Two Strings.
  3. Program to Check whether given String is Palindrome or not.
  4. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  5. Program to count total number of words in a string.
  6. Program to find smallest and largest word of the String.
  7. Program to find Most Frequent Character of the String.
  8. Program to Remove all the blank spaces from the String.
  9. Program to check if String is isogram or not.
  10. Program to Reverse Each word of the String.
  11. Program to Print All the Substring of the Given String.
  12. Program to find longest palindromic Substring.
  13. Program to check Anagram Strings.
  14. Program to check whether two Strings are Rotation of each other or not.
  15. Program to check Palindromic Anagram.
  16. Program to print all the Palindromic Substring of the String.
  17. Program to check Panagram String.
  18. Program to find first non-repeating character of the String.
  19. Program to find equilibrium index of an Array.
  20. Program to find majority element of an Array.

You may also like...