Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Mahul Sethi 200102001 1000014223

DIT University, Dehradun

School of Computing

CSF-401 Compiler Design

Graded Lab-1

Q-1. Write a program to count number of characters, space and


digits in a line.
Code:
#include
<stdio.h>
#include
<string.h
>#include
<stdlib.h
> #define
str_size
100

void main()
{ char
str[str_size
];int alp,
digit, spch,
i; alp =
digit = i =
0;
spch=-1;

printf("\nCount total number of alphabets, digits and special


characters :\n");printf("Enter the string : "); fgets(str, sizeof str,
stdin);

while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alp++;
}
else if(str[i]>='0' && str[i]<='9')
Mahul Sethi 200102001 1000014223

{
digit++;
}
else {
spch++;
}

i++; }

printf("Number of Alphabets in the string is : %d\n", alp);

printf("Number of Digits in the string is : %d\n", digit);

printf("Number of Space in the string is : %d\n\n", spch);


}

Output:
Mahul Sethi 200102001 1000014223

Q.2. Write a program to identify whether any given line is a


comment or not.
Code:
#include
<stdio.h>
#include
<string.h>

int main() {

char str[50]; int idx = 2, x = 0;

printf("Enter the
String: ");
scanf("%[^\n]s",
&str);

printf("\n

Output:");

if (str[0]

== '/') {

if (str[1] == '/') { printf("Comment");


} else if (str[1] == '*') { for (idx = 2; idx <=
50; idx++) { if (str[idx] == '*'
&& str[idx

+ 1] == '/') { printf("Comment");
x = 1;

break; }
else

Continue
Mahul Sethi 200102001 1000014223

if
(x == 0) {
printf("Err
or");

} else {

printf("Error");

} else {
printf("Er
ror"); }

return 0;

Output
Mahul Sethi 200102001 1000014223

You might also like