0% found this document useful (0 votes)
43 views2 pages

#Include #Include #Include #Include

The document contains a C program that reads user input from a file, identifies numbers, keywords, and identifiers, and counts the number of lines. It uses functions to recognize and categorize different components of the C code. The output includes identified numbers, keywords, identifiers, special characters, and the total number of lines in the input program.

Uploaded by

Crack Tunes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

#Include #Include #Include #Include

The document contains a C program that reads user input from a file, identifies numbers, keywords, and identifiers, and counts the number of lines. It uses functions to recognize and categorize different components of the C code. The output includes identified numbers, keywords, identifiers, special characters, and the total number of lines in the input program.

Uploaded by

Crack Tunes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Asad Nasir

18-SE-37

Code
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
FILE *f1;
char c,str[10];
int lineno=1,num=0,i=0;
clrscr();
printf("\nEnter the c program\n");
f1=fopen("input.txt","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input.txt","r");
while((c=getc(f1))!=EOF) // TO READ THE GIVEN FILE
{
if(isdigit(c)) // TO RECOGNIZE NUMBERS
{
num=c-48;
c=getc(f1);
while(isdigit(c))
{
num=num*10+(c-48);
c=getc(f1);
}
printf("%d is a number \n",num);
ungetc(c,f1);
}
else if(isalpha(c)) // TO RECOGNIZE KEYWORDS AND IDENTIFIERS
{
str[i++]=c;
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
str[i++]=c;
c=getc(f1);
}
str[i++]='\0';
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||
strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||
strcmp("double",str)==0||strcmp("static",str)==0||
Asad Nasir
18-SE-37

strcmp("switch",str)==0||strcmp("case",str)==0) // TYPE 32 KEYWORDS


printf("%s is a keyword\n",str);Blog - https://anilkumarprathipati.wordpress.com
/ 6
else
printf("%s is a identifier\n",str);
ungetc(c,f1);
i=0;
}
else if(c==' '||c=='\t') // TO IGNORE THE SPACE
printf("\n");
else if(c=='\n') // TO COUNT LINE NUMBER
lineno++;
else // TO FIND SPECIAL SYMBOL
printf("%c is a special symbol\n",c);
}
printf("Total no. of lines are: %d\n",lineno);
fclose(f1);
getch();
}

Output
Enter the c program
int main()
{
int a=10,20;
charch;
float f;
}^Z
The numbers in the program are: 10 20
The keywords and identifiersare:
int is a keyword
main is an identifier
int is a keyword
a is an identifier
char is a keyword
ch is an identifier
float is a keyword
f is an identifier
Special characters are ( ) { = , ; ; ; }
Total no. of lines are:5

You might also like