Created
August 9, 2020 04:02
-
-
Save parzibyte/04946a4209ecf77e378c5a744a1152bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int esEnteroValido(char cadena[LONGITUD_CADENA]) { | |
int longitud = strlen(cadena); | |
// Quitar espacios, saltos de línea, etcétera | |
while (longitud > 0 && isspace(cadena[longitud - 1])) | |
longitud--; | |
if (longitud <= 0) return 0; | |
int i; | |
for (i = 0; i < longitud; ++i) { | |
// En caso de que sea un guión, y que no esté al inicio, no es válido | |
if (cadena[i] == '-' && i > 0) { | |
return 0; | |
} | |
// Si no es dígito, tampoco es válido | |
if (!isdigit(cadena[i]) && cadena[i] != '-') { | |
return 0; | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment