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:
- Initialize two flag variables, namely, alphabetic and numeric with 0.
- Scan the complete string and check whether encountered character is alphabet or number.
- If encountered character is alphabet, set flag variable alphabetic with 1, else if encountered character is number, set flag variable numeric with 1.
- If alphabetic is set to 1 and numeric is set to 0, then given string is alphabetic string.
- If alphabetic is set to 0 and numeric is set to 1, then given string is numeric string.
- 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:
- Program to copy one String to Another.
- Program to Concatenate Two Strings.
- Program to Check whether given String is Palindrome or not.
- Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
- Program to count total number of words in a string.
- Program to find smallest and largest word of the String.
- Program to find Most Frequent Character of the String.
- Program to Remove all the blank spaces from the String.
- Program to check if String is isogram or not.
- Program to Reverse Each word of the String.
- Program to Print All the Substring of the Given String.
- Program to find longest palindromic Substring.
- Program to check Anagram Strings.
- Program to check whether two Strings are Rotation of each other or not.
- Program to check Palindromic Anagram.
- Program to print all the Palindromic Substring of the String.
- Program to check Panagram String.
- Program to find first non-repeating character of the String.
- Program to find equilibrium index of an Array.
- Program to find majority element of an Array.