Check whether the given string is a valid identifier
Last Updated :
07 Dec, 2022
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 an 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.
Examples:
Input: str= “_geeks123”
Output: Valid
Input: str = “123geeks_”
Output: Invalid
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:
C++
#include <bits/stdc++.h>
using namespace std;
bool isValid(string str, int n)
{
if (!((str[0] >= 'a' && str[0] <= 'z' )
|| (str[0] >= 'A' && str[0] <= 'Z' )
|| str[0] == '_' ))
return false ;
for ( int i = 1; i < str.length(); i++) {
if (!((str[i] >= 'a' && str[i] <= 'z' )
|| (str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= '0' && str[i] <= '9' )
|| str[i] == '_' ))
return false ;
}
return true ;
}
int main()
{
string str = "_geeks123" ;
int n = str.length();
if (isValid(str, n))
cout << "Valid" ;
else
cout << "Invalid" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isValid(String str, int n)
{
if (!((str.charAt( 0 ) >= 'a' && str.charAt( 0 ) <= 'z' )
|| (str.charAt( 0 )>= 'A' && str.charAt( 0 ) <= 'Z' )
|| str.charAt( 0 ) == '_' ))
return false ;
for ( int i = 1 ; i < str.length(); i++)
{
if (!((str.charAt(i) >= 'a' && str.charAt(i) <= 'z' )
|| (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z' )
|| (str.charAt(i) >= '0' && str.charAt(i) <= '9' )
|| str.charAt(i) == '_' ))
return false ;
}
return true ;
}
public static void main(String args[])
{
String str = "_geeks123" ;
int n = str.length();
if (isValid(str, n))
System.out.println( "Valid" );
else
System.out.println( "Invalid" );
}
}
|
Python3
def isValid(str1, n):
if ((( ord (str1[ 0 ]) > = ord ( 'a' ) and
ord (str1[ 0 ]) < = ord ( 'z' )) or
( ord (str1[ 0 ]) > = ord ( 'A' ) and
ord (str1[ 0 ]) < = ord ( 'Z' )) or
ord (str1[ 0 ]) = = ord ( '_' )) = = False ):
return False
for i in range ( 1 , len (str1)):
if ((( ord (str1[i]) > = ord ( 'a' ) and
ord (str1[i]) < = ord ( 'z' )) or
( ord (str1[i]) > = ord ( 'A' ) and
ord (str1[i]) < = ord ( 'Z' )) or
( ord (str1[i]) > = ord ( '0' ) and
ord (str1[i]) < = ord ( '9' )) or
ord (str1[i]) = = ord ( '_' )) = = False ):
return False
return True
str1 = "_geeks123"
n = len (str1)
if (isValid(str1, n)):
print ( "Valid" )
else :
print ( "Invalid" )
|
C#
using System;
class GFG
{
static bool isValid(String str, int n)
{
if (!((str[0] >= 'a' && str[0] <= 'z' )
|| (str[0] >= 'A' && str[0] <= 'Z' )
|| str[0] == '_' ))
return false ;
for ( int i = 1; i < str.Length; i++)
{
if (!((str[i] >= 'a' && str[i] <= 'z' )
|| (str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= '0' && str[i] <= '9' )
|| str[i] == '_' ))
return false ;
}
return true ;
}
public static void Main(String []args)
{
String str = "_geeks123" ;
int n = str.Length;
if (isValid(str, n))
Console.WriteLine( "Valid" );
else
Console.WriteLine( "Invalid" );
}
}
|
PHP
<?php
function isValid( $str , $n )
{
if (!(( $str [0] >= 'a' && $str [0] <= 'z' ) ||
( $str [0] >= 'A' && $str [0] <= 'Z' ) ||
$str [0] == '_' ))
return false;
for ( $i = 1; $i < strlen ( $str ); $i ++)
{
if (!(( $str [ $i ] >= 'a' && $str [ $i ] <= 'z' ) ||
( $str [ $i ] >= 'A' && $str [ $i ] <= 'Z' ) ||
( $str [ $i ] >= '0' && $str [ $i ] <= '9' ) ||
$str [ $i ] == '_' ))
return false;
}
return true;
}
$str = "_geeks123" ;
$n = strlen ( $str );
if (isValid( $str , $n ))
print ( "Valid" );
else
print ( "Invalid" );
?>
|
Javascript
<script>
function isValid(str,n)
{
if (!((str[0] >= 'a' && str[0] <= 'z' )
|| (str[0]>= 'A' && str[0] <= 'Z' )
|| str[0] == '_' ))
return false ;
for (let i = 1; i < str.length; i++)
{
if (!((str[i] >= 'a' && str[i] <= 'z' )
|| (str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= '0' && str[i] <= '9' )
|| str[i] == '_' ))
return false ;
}
return true ;
}
let str = "_geeks123" ;
let n = str.length;
if (isValid(str, n))
document.write( "Valid" );
else
document.write( "Invalid" );
</script>
|
Time Complexity: O(n), where n is length of the given string
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...