An identifier in C, Java, C++ and most other programming languages must begin with a letter and then may be followed by any number of letters or digits. It is possible that underscores (_) will also appear, but only in the middle and never two consecutively. Write a program to read a string and output whether it is a valid or invalid identifier. Each string will be 10 characters or less in size. Test Case 1 Input (stdin) UAB_HSPC Expected Output valid identifier Test Case 2 Input (stdin) 2ab Expected Output not a valid identifier
#include<stdio.h>
#include<string.h>
int main()
{
char string[25];
int count=0,flag;
int i;
scanf ("%[^\n]s",string);
if( (string[0]>='a'&&string[0]<='z')
||
(string[0]>='A'&&string[0]<='Z')
||
(string[0]=='_')
)
{
for(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("not a valid identifier");
else
printf("valid identifier");
return 0;
}