Beruflich Dokumente
Kultur Dokumente
Lab 06
Checking Data type, Operator validity through tokenizing input
string
As we have seen, C++ programs can be written using many English words. It is useful to
think of words found in a program as being one of three types:
• Reserved Words. These are words such as if, int and else, which have a predefined
meaning that cannot be changed. Here's a more complete list.
• Library Identifiers. These words are supplied default meanings by the programming
environment, and should only have their meanings changed if the programmer has
strong reasons for doing so. Examples are cin, cout and sqrt (square root).
• Programmer-supplied Identifiers. These words are "created" by the programmer, and
are typically variable names, such as year_now and another_age.
An identifier cannot be any sequence of symbols. A valid identifier must start with a letter of
the alphabet or an underscore ("_") and must consist only of letters, digits, and underscores.
Program Logic
Program
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char a[10];
int flag, i=1;
clrscr();
printf("\n Enter an identifier:");
gets(a);
if(isalpha(a[0])) flag=1;
else
printf("\n Not a valid identifier");
while(a[i]!='\0')
{
if(!isdigit(a[i])&&!isalpha(a[i])) {
flag=0;
break;
}
i++;
}
if(flag==1)
printf("\n Valid identifier");
getch();
}
Result
Output:
Valid identifier
Enter an identifier: 1aqw
Task: Write a C program to test whether a given Data Type and Operator is valid or not.