Lab Manual 1
Lab Manual 1
Lab Manual 1
LAB MANUAL
SYSTEM PROGRAMMING LAB
B.Tech–IV Semester
INDEX
Sr. No. Experiments
Practical 1
Aim: Write a program to create, read and write into a file having record of students.
Theory:
Opening a file
$filename='c:\file.txt':
$fp = fopen($filename."r"):
the fopen() function normally uses two parameters. the first is the file which we want to open.
this can be a file on your system. in this example we are opening a text file on a windows
machine. the second parameter is the "mode". we can open a file for reading and writing. in
the above example "r" means reading.
After we have opened the file, we will probably want to do something with it. to read the
contents of a file into a variable we use fread().
$contents=fread($fp, filesize($filename));
fclose($fp);
Now fread() also uses two parameters. the first parameter is the variable to which we
assigned the fopen() function, the second is the number of bytes we want to read up to in the
file. in this case we want to read the entire file, so we use the filesize() function to get the size
of the file specified in the $filename variable. thus it reads the entire file in.
line1
line2
line 3
line4
line5
$contents="line1\nline2\nline3\nline4\nline5":
line1
line2
line3
line4
line5
The contents of the file are read into a string, complete with newline characters(\n. we can
then process this string however we like.
Write to a file:
We can also write data into a file once we have opened it. to do this we use the fputs()
function.
$filename = 'c:\file.txt':
$fp = fopen($filename,"a");
$string="\nline5";
$write=fputs($fp.$string);
fclose($fp);
firstly we open the file. notice the "a" parameter? that means "open the file for writing only,
and place the file pointer at the end of the file".
we then use fputs() to write to the file. we then close the file. so now what will the file
contain.
line1
line2
line3
line4
modes:
there are various modes you can use to open a file, they are listed on the php.net fopen()
function page, but i'll stick them up on here too.
'r'-open for reading only, place the place the file pointer at the beginning of the file.
'r+'- open for reading and writing ; place the file pointer at the beginning of the file.
'w' – open for writing only, place the file pointer at the beginning of the file and
truncate the file to zero length. if the file does not exist, attempt to create it.
'a' – open for writing only, place the file pointer at the end of the file. if the file does
not exist, attempt to create it.
'a+' – open for reading and writing ; place the file pointer at the end of the file. if the
file does not exist , attempt to create it.
VIVA VOICE:
Practical 2
Aim: Write a program to count the numbers of vowels and consonants in a given string.
#include<stdio.h>
//#include<conio.h>
#include<string.h>
void main()
{
char str[50];
int vovels=0,consonants=0,len,i;
printf("Enter the String:\n");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if((str[i]>64&&str[i]<91)||(str[i]>96&&str[i]<123)) //Firstly checking the
character is alphabet or not
{
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]
=='u'||str[i]=='U') //checking if it is vowel or not
vovels++;
else
consonants++;
}
}
printf("Number of vovels = %d and consonants = %d ",vovels,consonants);
//getch();
}
O/P:
Practical 3
Aim: Program to count the number of characters, words of a given input string.
#include <stdio.h>
void main()
{
int countch=0;
int countwd=1;
printf("Characters = ",countch-1);
getch();
Practical 4
Algorithm:
Step 2: create a structure for opcode table and assign the values.
Step 3: create a structure for symbol table and assign the values.
Step 4: create a structure for intermediate code table and assign the values.
Step 5: write the opcode in separate file and machine code in another separate file.
Step 6: open the opcode file and compare it with the given machine code and then generate
Step 7: check the forward reference in intermediate code and print the corresponding jump
statement address.
Step 8: compare machine code with the opcode. If any jump statement with backward
Step 9: for symbol table, print the symbol and address of the symbol.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
char var[10];
int value;
};
int i,j,n;
void create();
void modify();
void insert();
void display();
void main()
int ch,result=0;
char v[10];
clrscr();
do
scanf("%d",&ch);
switch(ch)
case 1:
create();
break;
case 2;
insert();
break;
case 3:
modify();
break;
case 4:
scanf("%s",&v);
result=search(v,n);
if(result==0)
else
break;
case 5:
display();
break;
case 6;
exit(1);
while(ch!=6)
getch();
void create()
scanf("%d",&n);
scanf("%s%d",tbl[i].var,&tbl[i].value);
check:
printf("the variable should start with an alphabet\n enter the correct variable name \n");3
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check;
check1:
if(strcmp(tbl(i).var,tbl[j].var==0)
scanf("%s%d",tbl[i].var,&tbl[i].value);
goto check 1;
display();
void insert()
if(i>=20)
else
n++;
scanf("%s%d",tbl[n].var,&tbl[n].value);
check:
printf("the variable should start with alphabet \n enter the correct variable name \n");
goto check;
check 1:
if(strcmp(tbl[j].var,tbl[i].var)==0)
scanf("%s%d",tbl[i].var, &tbl[i].value);
goto check 1;
display();
void modify(0
char variable[10];
int result = 0;
if(result==0)
else
printf("the current value of the variable %s is %d, enter the new variable and its value",
tbl[result].var, tbl[result].value);
printf("the variable should start with alphabet \n enter the correct variable name\n");
goto check;
display();
int flag;
for (i = 1; i<=n;i++)
if (strcmp(tbl[i].var, variable)==0){
flag = 1;
break;
if (flag==1)
return i;
else
return 0;
void display()
printf("%s\t\t%d\n",tbl[i].var,tbl[i].value);
Output:
Enter ur choice:
1. Create
2. Insert
3. Modify
4. Search
5. Display
6. Exit
1.
2.
A 26
B 42
Variable Value
A 25
B 42w
Enter ur choice:
1. Create
2. Insert
3. Modify
4. Search
5. Display
6. Exit
2.
D 10
Variable Value
A 26
B 42
D 10
Enter ur choice:
1. Create
2. Insert
3. Modify
4. Search
5. Display
6. Exit
The current value of variable D is 10, Enter the new variable and its value
20
Variable Value
A 26
B 42
C 20
Enter ur choice:
1. Create
2. Insert
3. Modify
4. Search
5. Display
6. Exit
2. Insert
3. Modify
4. Search
5. Display
6. Exit
Variable Value
A 26
B 42
C 20
Enter ur choice:
1. Create
2. Insert
3. Modify
4. Search
5. Display
6. Exit
Result:
Thus the program has been done and the output has been verified.
Practical 5
ALGORITHM:
Step 2: Assembler simply generate object code as it scans the source code.
Step 3: If the instruction operand is a symbol text, has not yet been defined, the operands
address is omitted.
Step 4: When nearly half the program translation is over, some of the forward reference
Step 5: Combine the process to the end of the program to fill forward reference property.
Step 6: At the end of the program, the symbol table entries with 'x' are undefined.
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main
int i, j, num, q, r;
clrscr();
f1 = fopen("asin","w");
while(1)
ch = getchar();
if (ch=='*')
break;
fputec(ch, f1);
fclose(f1);
f1 = fopen("asin", "r");
f2 = fopen("asout", "w");
while (1)
strncpystr1, str, 3;
str [] = '\0';
j = 0;
str2[j] = str[i];
j++;
str[j] = '\0';
if ((strcmp(str1, "1da"))==0)
fputs("3a\t",f2);
fputs(str2, f2);
fputs("47 \n",f2);
else if ((strcmp(str1,"add"))==0)
fputs("80 \n",f2);
break:
num = atoi(str2)
q = num/100;
r = num%100;
if(r==0)
fputs("00 \t",f2);
else
fputs(itoa(r,cstr,10),f2);
fputs("\t",f2);
fputs(itoa(q, cstr,10),f2);
fputs("\n",f2);
else
fputs("error\n",f2);
fclose(f1);
fclose(f2);
f2 = fopen("asout", "r");
ch = fgetc(f2);
while(ch! = eof)
putchar(ch);
ch = fgetc(f2);
fclose(f2);
getch();
Output:
Input
Lda 5000
Sub z
Sta 9988
Hlt
Output
3a 00 50
90
32 88 99
76
Result
Thu the program has been done and the output has been verified.
Algorithm:
Step 1: Start
Step 6: Stop
Practical 6
#include<stdio.h>
#include<conio.h>
#include<string.h>
char *p;
e();
t();
main()
int i, j = 0, n b[10], k = 0;
clrscr();
scanf("%s", a);
switch(a[i])
case '+':
case '-':
c[j] = a[i];
b[j] = 1;
j++;
break;
case '*':
case '/':
c[j]= a[i];
b[j] = 2;
j++;
break;
case'^':
c[j] = a[i];
b[j] = 3;
j++;
break;
default:
if(k==0)
k = 1;
c[j] = a[i];
b[j] = 4;
j++;
c[j] = '$';
c[j] = 0;
j+;
printf(\n\n");
printf(\n\t------------------------------------------------");
printf("\n\n");
printf("\t%c",c[i]);
printf('\t");
printf("\n\t------------------------------------------------");
printf("\n\n%c",c[i]);
for(n=0; n<j;n++)
if(b[i]<b[n])
printf("\t<");
if(b[i>b[n])
printf("\t>");
if(b[i]==b[n])
printf("\t=");
printf("\t");
printf("\n\t------------------------------------------------");
p = a;
if(e())
else
getch();
return 0;
int e()
if(*p=='i')
p++;
t();
t();
else
return 0;
int t()
if(*p==nul)
return 1;
else(if*p=='+'||*p=='*')
p++;
if(*p=='i')
p++;
else
return 0;
else
return 0;
Output
|+|*i
------------------------------------------------
i + * $
------------------------------------------------
------------------------------------------------
------------------------------------------------
------------------------------------------------
------------------------------------------------
String parsed
Practical 7
Lexical Analyzer
Aim: To write a program for diving the given input program into lexemes.
Algorithm:
Step 1: Start
Step 2: Read the file and open the file as read mode.
Step 6: Stop
Practical 7
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
int i,j,k,p,c;
char key[9][10]={"main","if","else","switch","void","do","while","for","return"};
char opr[5]=:'*','+','-','/','^'};
file *fp;
clrscr();
scanf("%s",s);
fp = fopen(s,"r");
c=0;
do
fscanf(fp, "%s",r);
getch()]
if(strchr(r,par[i])!=null)
for(i=+;i<9;i++)
ifstrstr(r,key[i])!=null)
for (i=0;i<4;i++)
if((strstr(r,dat[i])&&(!strstr(r,"printf")))!=null)
if (strchr(r, opr[i])!=null)
p = c;
c = ftel(fp);