Wednesday 9 March 2016

Identify whether identifier is valid or not

1


An identifier is used for any variable, function, data definition, etc. In the programming language C, an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline.

This program is to identify whether the entered input is a valid identifier or not. Here goes the code:

#include<stdio.h> 
#include<conio.h> 
#include<ctype.h> // to use isdigit() and isalpha() functions
void main() {    
char a[10];  
int flag, i=1; 
for(int j=0; j<5; j++) {
    printf("\n Enter an identifier:");  
    gets(a);    
    if(isalpha(a[0]) || a[0]=='_')      
        flag=1;  
    else      
        printf("\n Not a valid identifier");    
    while(a[i]!='\0')    {   
        if(!isdigit(a[i])&&!isalpha(a[i])&&a[i]!='_') {         
            flag=0;         
            break;       
        }     
        i++;  
    }  
    if(flag==1)     
    printf("\n Valid identifier");    
    }
 


Output: