2009-09-25 13 views
5

में एक पत्र या संख्या इनपुट करता है, क्या यह देखने के लिए कोई सी आसान तरीका है कि उपयोगकर्ता अंग्रेजी वर्णमाला से एक पत्र इनपुट करता है या नहीं? मैं इस तरह कुछ सोच रहा हूं:जांचें कि क्या उपयोगकर्ता इनपुट 0 या

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

मैं यह सुनिश्चित करने के लिए जांचना चाहता हूं कि उपयोगकर्ता एक पत्र दर्ज नहीं करता है, बल्कि इसके बजाय एक नंबर दर्ज करता है। ताज्जुब है कि वहाँ एक आसान तरीका मैन्युअल वर्णमाला :)

उत्तर

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

+1, लेकिन कृपया '$' से '#' ;-) –

+4

को ध्यान में रखते हुए ध्यान दें कि 'isdigit()' यहां अधिक उपयोगी है। –

+0

बहुत बढ़िया। संख्या और अल्फा की जांच करना क्या होगा? –

13

यह सबसे अच्छा दशमलव अंकों की खुद को बजाय पत्र के लिए परीक्षण करने के लिए है के प्रत्येक अक्षर में लिखे बिना हर पत्र खींचने के लिए है। isdigit

#include <ctype.h> 

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

isalpha समारोह के अलावा, आप इसे इस तरह कर सकते हैं:

char vrbl; 

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

हालांकि यह अभी भी ASCII रेंज के बाहर अक्षरों को अनदेखा करने की अनुमति देता है ... – AlBlue

+0

स्वरों के खिलाफ आपके पास क्या है? यदि आप अपने परिवर्तनीय नामों से अर्थ को हटाने जा रहे हैं, तो आप बस 'v' के साथ भी जा सकते हैं। –

1

isalpha() एक समय में एक चरित्र का परीक्षण होगा। यदि उपयोगकर्ता 23 ए 4 की तरह एक इनपुट इनपुट करता है, तो आप प्रत्येक पत्र का परीक्षण करना चाहते हैं। आप इसका उपयोग कर सकते हैं:

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 
} 

मैं मानता हूं कि atoi() थ्रेड सुरक्षित नहीं है और एक बहिष्कृत कार्य नहीं है। आप इसके स्थान पर एक और सरल कार्य लिख सकते हैं।

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*() पुस्तकालय कार्यों यहाँ काम में आते हैं:

#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

तुम भी check whether a character is alphabet or not

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

लिए कुछ सरल शर्तों के साथ यह कर सकते हैं या आप भी ASCII मूल्यों

उपयोग कर सकते हैं
if((ch>=97 && ch<=122) || (ch>=65 && ch<=90)) 
{ 
    printf("Alphabet"); 
} 
संबंधित मुद्दे