Dada una string str , la tarea es verificar si la string es un identificador válido o no. Para calificar como un identificador válido, la string debe cumplir las siguientes condiciones:
- Debe comenzar con un guión bajo (_) o cualquiera de los caracteres de los rangos [‘a’, ‘z’] y [‘A’, ‘Z’] .
- No debe haber ningún espacio en blanco en la string.
- Y, todos los caracteres subsiguientes después del primer carácter no deben consistir en ningún carácter especial como $ , # , % , etc.
Ejemplos:
Entrada: str= “_geeks123”
Salida: Válido
Entrada: str = “123geeks_”
Salida: No válido
Enfoque: recorra la string carácter por carácter y compruebe si se cumplen todos los requisitos para que sea un identificador válido, es decir, el primer carácter solo puede ser ‘_’ o un alfabeto inglés y el resto de los caracteres no debe ser un espacio en blanco o cualquier carácter especial.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if str // is a valid identifier bool isValid(string str, int n) { // If first character is invalid if (!((str[0] >= 'a' && str[0] <= 'z') || (str[0] >= 'A' && str[0] <= 'Z') || str[0] == '_')) return false; // Traverse the string for the rest of the characters 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; } // String is a valid identifier return true; } // Driver code int main() { string str = "_geeks123"; int n = str.length(); if (isValid(str, n)) cout << "Valid"; else cout << "Invalid"; return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if str // is a valid identifier static boolean isValid(String str, int n) { // If first character is invalid if (!((str.charAt(0) >= 'a' && str.charAt(0) <= 'z') || (str.charAt(0)>= 'A' && str.charAt(0) <= 'Z') || str.charAt(0) == '_')) return false; // Traverse the string for the rest of the characters 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; } // String is a valid identifier return true; } // Driver code 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"); } } // This code is contributed by SURENDRA_GANGWAR
Python3
# Python3 implementation of the approach # Function that returns true if str1 # is a valid identifier def isValid(str1, n): # If first character is invalid 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 # Traverse the for the rest of the characters 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 # is a valid identifier return True # Driver code str1 = "_geeks123" n = len(str1) if (isValid(str1, n)): print("Valid") else: print("Invalid") # This code is contributed by mohit kumar
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if str // is a valid identifier static bool isValid(String str, int n) { // If first character is invalid if (!((str[0] >= 'a' && str[0] <= 'z') || (str[0] >= 'A' && str[0] <= 'Z') || str[0] == '_')) return false; // Traverse the string for the rest of the characters 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; } // String is a valid identifier return true; } // Driver code public static void Main(String []args) { String str = "_geeks123"; int n = str.Length; if (isValid(str, n)) Console.WriteLine("Valid"); else Console.WriteLine("Invalid"); } } // This code contributed by Rajput-Ji
PHP
<?php // PHP implementation of the approach // Function that returns true if str // is a valid identifier function isValid($str, $n) { // If first character is invalid if (!(($str[0] >= 'a' && $str[0] <= 'z') || ($str[0] >= 'A' && $str[0] <= 'Z') || $str[0] == '_')) return false; // Traverse the string // for the rest of the characters 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; } // String is a valid identifier return true; } // Driver code $str = "_geeks123"; $n = strlen($str); if (isValid($str,$n)) print("Valid"); else print("Invalid"); // This code is contributed by Ryuga ?>
Javascript
<script> // Javascript implementation of the approach // Function that returns true if str // is a valid identifier function isValid(str,n) { // If first character is invalid if (!((str[0] >= 'a' && str[0] <= 'z') || (str[0]>= 'A' && str[0] <= 'Z') || str[0] == '_')) return false; // Traverse the string for the rest of the characters 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; } // String is a valid identifier return true; } // Driver code let str = "_geeks123"; let n = str.length; if (isValid(str, n)) document.write("Valid"); else document.write("Invalid"); // This code is contributed by patel2127 </script>
Valid
Publicación traducida automáticamente
Artículo escrito por shubham_singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA