-->

Header Ads

C Program to check Whether Entered String is Identifier or Variable.

A string is called identifier when he follows these rules:

1. The first letter must be alphabet(both capital or small i.e. A-Z,a-z) or underscore(_).
2. After first letter it contains sequence of alphabet or digits(0-9) or underscore(_)
but not contain any special symbol(#,$,%,^,& etc.) and space( ).

C Program:


#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

clrscr();

char string[25];

int count=0,flag;

printf("enter any string: ");

gets(string);

if( (string[0]>='a'&&string[0]<='z') //small letter

    ||

    (string[0]>='A'&&string[0]<='Z') //cap letter

    ||

    (string[0]=='_')   //underscore

   )

       {



   for(int i=1;i<=strlen(string);i++)

   {

     if((string[i]>='a'&& string[i]<='z')

     ||

      (string[i]>='A' && string[i]<='Z')

     ||

     (string[i]>='0'&& string[i]<='9')

     ||

     (string[i]=='-')

     )

     {

 count++;



     }

   }

    if(count==strlen(string))

    {

      flag=0;

    }

  }

 else

 {

  flag=1;

 }



if(flag==1)

 printf("%s is not valid identifier",string);

 else

 printf("%s is valid identifier",string);



getch();

}

Output:


No comments

Powered by Blogger.