Given a string str, the task is to check if the string is a valid identifier or not. In order to qualify as a valid identifier, the string must satisfy the following conditions:
- It must start with either underscore(_) or any of the characters from the ranges [‘a’, ‘z’] and [‘A’, ‘Z’].
- There must not be any white space in the string.
- And, all the subsequent characters after the first character must not consist of any special characters like $, #, % etc.
Input: str= “_geeks123”
Input: str = “123geeks_”
Approach: Traverse the string character by character and check whether all the requirements are met for it to be a valid identifier i.e. first character can only be either ‘_’ or an English alphabet and the rest of the characters must neither be a white space or any special character.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Check whether the triangle is valid or not if angles are given
- Check whether Quadrilateral is valid or not if angles are given
- Check if an URL is valid or not using Regular Expression
- Program to check for a Valid IMEI Number
- How to check Aadhar number is valid or not using Regular Expression
- Program to generate all possible valid IP addresses from given string
- Program to generate all possible valid IP addresses from given string | Set 2
- Difference between Keyword and Identifier
- How to validate identifier using Regular Expression in Java
- How to validate GUID (Globally Unique Identifier) using Regular Expression
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if any permutation of string is a K times repeated string
- Check if left and right shift of any string results into given string
- Check if a string can become empty by recursively deleting a given sub-string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.