Professional Documents
Culture Documents
181-15-961 - 59 - A1 (Lab - 5) - Compiler - Design
181-15-961 - 59 - A1 (Lab - 5) - Compiler - Design
Submitted To:
Fahim Faisal
Lecturer
Department of Computer Science and Engineering
Daffodil International University
Submitted By:
Name: Sabrina Sharmin
ID: 181-15-961
Section: 59_A1
Program: CSE
Title: Write a C program to take a list of identifiers from the user and display the
validity of each identifier.
Objective:
The objective of this program is to validate each string entered by the user, separated
by spaces. It checks if each string conforms to the rules for a valid identifier in C, which
requires that the first character must be a letter (uppercase or lowercase), underscore
(_), or dollar sign ($), and subsequent characters can also include digits (0-9). Any
string not meeting these criteria is considered invalid. The program prints whether each
string entered is valid or not.
Program Logic:
● The program takes input from the user as a string and processes each word
separated by spaces.
● It iterates through each character of the word, validating whether it meets the
criteria for a valid identifier in C.
● The validation function examines each character in the string to ensure it falls
within the acceptable range of characters for an identifier.
● If any character violates the rules, the string is marked as invalid; otherwise, it's
considered valid.
● The program prints the validity status of each string entered by the user.
Procedure:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void test(char str[100]) {
int c = 1, i, n;
n = strlen(str);
if (!((str[0] >= 'a' && str[0] <= 'z') || (str[0] >= 'A' && str[0] <= 'Z') || str[0] == '_' || str[0]
== '$')) {
c = 0;
}
if (!((str[n - 1] >= 'a' && str[n - 1] <= 'z') || (str[n - 1] >= 'A' && str[n - 1] <= 'Z') || str[n -
1] == '$' || (str[n - 1] >= '0' && str[n - 1] <= '9'))) {
c = 0;
}
if (c == 1) {
printf("'%s' is Valid \n", str);
} else {
printf("'%s' is Invalid \n", str);
}
}
void main() {
int i, n, j=0;
char cr[100];
printf("Enter your string: ");
gets(cr);
n = strlen(cr);
if (j > 0) {
test(str);
}
Result:
Conclusion: