2009-09-25 21 views
5

Czy istnieje łatwy sposób wywołania skryptu C, aby sprawdzić, czy użytkownik wprowadza literę z alfabetu angielskiego? Ja myślę tak:Sprawdź, czy użytkownik wpisuje literę lub numer w C

if (variable == a - z) {printf("You entered a letter! You must enter a number!");} else (//do something} 

Chcę sprawdzić, aby upewnić się, że użytkownik nie wprowadzić literę, ale wchodzi szereg zamiast. Zastanawiasz się, czy nie jest to łatwy sposób wyciągnąć każdy list bez konieczności ręcznego wpisywania każdej litery alfabetu :)

Odpowiedz

9
#include <ctype.h> 
if (isalpha(variable)) { ... } 
+2

+1, ale zmień '$' na '#' ;-) –

+4

Warto zauważyć, że 'isdigit()' jest tutaj bardziej użyteczny. –

+0

Awesome. Jaka byłaby kontrola liczby i alfa? –

13

Najlepiej przetestować sami dziesiętnych cyfr zamiast liter. isdigit.

#include <ctype.h> 

if(isdigit(variable)) 
{ 
    //valid input 
} 
else 
{ 
    //invalid input 
} 
1

Oprócz funkcji isalpha, można zrobić to tak:

char vrbl; 

if ((vrbl >= 'a' && vrbl <= 'z') || (vrbl >= 'A' && vrbl <= 'Z')) 
{ 
    printf("You entered a letter! You must enter a number!"); 
} 
+1

Mimo to nadal można ignorować litery spoza zakresu ASCII ... – AlBlue

+0

Co masz przeciwko samogłoskom? Jeśli chcesz usunąć znaczenie ze swoich nazw zmiennych, równie dobrze możesz użyć "v". –

1

isalpha() będzie testować jeden znak naraz. Jeśli użytkownik wprowadzi liczbę taką jak 23A4, wówczas chcesz przetestować każdą literę. Możesz użyć tego:

bool isNumber(char *input) { 
    for (i = 0; input[i] != '\0'; i++) 
     if (isalpha(input[i])) 
      return false; 
    return true; 
} 

// accept and check 
scanf("%s", input); // where input is a pointer to a char with memory allocated 
if (isNumber(input)) { 
    number = atoi(input); 
    // rest of the code 
} 

Zgadzam się, że funkcja atoi() nie jest bezpieczna dla wątków i jest przestarzałą funkcją. Zamiast tego możesz napisać inną prostą funkcję.

0
int strOnlyNumbers(char *str) 
{ 
char current_character; 
/* While current_character isn't null */ 
while(current_character = *str) 
{ 
    if(
    (current_character < '0') 
    || 
    (current_character > '9') 
    ) 
    { 
    return 0; 
    } 
    else 
    { 
    ++str; 
    } 
} 
return 1; 
} 
0

strto*() funkcji biblioteki się przydać tutaj:

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#define SIZE ... 

int main(void) 
{ 
    char buffer[SIZE]; 
    printf("Gimme an integer value: "); 
    fflush(stdout); 
    if (fgets(buffer, sizeof buffer, stdin)) 
    { 
    long value; 
    char *check; 
    /** 
    * strtol() scans the string and converts it to the equivalent 
    * integer value. check will point to the first character 
    * in the buffer that isn't part of a valid integer constant; 
    * e.g., if you type in "12W", check will point to 'W'. 
    * 
    * If check points to something other than whitespace or a 0 
    * terminator, then the input string is not a valid integer. 
    */ 
    value = strtol(buffer, &check, 0); 
    if (!isspace(*check) && *check != 0) 
    { 
     printf("%s is not a valid integer\n", buffer); 
    } 
    } 
    return 0; 
} 
0

Można również zrobić to z kilku prostych warunków check whether a character is alphabet or not

if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) 
{ 
    printf("Alphabet"); 
} 

Albo można też używać wartości ASCII

if((ch>=97 && ch<=122) || (ch>=65 && ch<=90)) 
{ 
    printf("Alphabet"); 
} 
Powiązane problemy