Lab Manual: Department of Computer Science
Lab Manual: Department of Computer Science
LAB MANUAL
1
INDEX
1 UNIX COMMANDS
2 SHELL PROGRAMMING
2
2.5 Program to generate menu driven program 16
2.6 Program to find the square and cube of the first five numbers 18
3 C PROGRAMMING ON UNIX
3
UNIX COMMANDS
4
1.1. GENERAL PURPOSE COMMANDS
1. $ date “+ %d%m%y”
13/01/09
January 2009
3. $who
Use: Displays the number of users who are logged in to the network
4. $ who –b
Use : Display the last date and time of reboot of the UNIX server
5
5. $ who am i
Use : Identifies the user and lists the user names, terminal line, the date of
login.
6. $uname –a
Linux ubuntu 2.64-generic #1 SMP Thu Apr 10 13:25:23 UTC 2009 i686
GNU/Linux
7. $ man cat
9. $ login Username
Password:
6
1.2. DIRECTORY HANDLING COMMANDS
10. $pwd
/home/root
Use : Displays the current working directory
11. $ls –a
12. $ls –R
7
1.3. FILE HANDLING COMMANDS
15. $ cd stud
$root/stud
Unix is a open source operating system and its license free of cost
Ctr+d
Use : This counts the number of lines, characters and words in m.c file.
8
1.4. INPUT AND OUTPUT REDIRECTION
20. $date>testing
$cat testing
$cat test2
Information Technology
$cat test1>>test2
$cat test2
22. $ ls sh*
Shan
Suresh
SaiKumar.c
Shanthi.c
Radha
Asha
Usha
Sha
9
1.5. PIPES AND FILTERS
FREE OF COST. IT’S MAINLY USED FOR MAINFRAME AND HIGH SECURITY
NETWORKS
Use: To extract logins of people whose login names starts with “cse”
Welcome to C Language
Welcome to C++ Language
Welcome to Java Programming
Use: Head -3 is used to display the first three lines in the file top and Tail -3 is
used to display the last three lines in the file top.
10
SHELL PROGRAMMING
11
2.1. FAHRENHEIT TO CENTIGRADE AND VICE VERSA
AIM:
PROGRAM:
read c
read fh
OUTPUT
$ Sh Centigrade to Fahrenheit
RESULT
Thus the above shell program to convert Fahrenheit to centigrade and vice-versa
is been verified and executed successfully.
12
2.2. ODD OR EVEN
AIM
To write a shell program to check whether the given number is odd or even.
PROGRAM
read a
c= ’expr $a % 2’
if test $c –eq 0
then
else
fi
OUTPUT
$ Sh oddeven
RESULT
Thus the above shell program to check whether the number is odd or even is been
verified and executed successfully.
13
2.3. COMPARE TWO STRINGS
AIM
PROGRAM
read s1
read s2
if [ $s1 = $s2 ]
then
else
fi
OUTPUT
$ Sh stringcomp
RESULT
Thus the above shell program to compare the two given strings is been verified
and executed successfully.
14
2. 4. CHECK NUMBER IS POSITIVE OR NEGATIVE
AIM
PROGRAM
OUTPUT
$ Sh nopos
RESULT
Thus the above shell program to determine the number is positive, negative or
zero is been verified and executed successfully.
15
2. 5. MENU DRIVEN PROGRAM
AIM
PROGRAM
echo “ Menu”
echo “1. List of files”
echo “2. Todays Date”
echo “3. Process Status”
echo “4. Logged in Users”
echo “5. Quit”
echo –n “Enter your Choice: ”
read ch
case “$ch” in
1) ls –I;;
2) date;;
3) ps;;
4) who;;
5) exit;;
6) echo “Invalid Choice”
exit;;
esac
16
OUTPUT
$ Sh menu
13/01/09
RESULT
Thus the above shell program to determine to generate menu driven program is
been verified and executed successfully.
17
2. 6. SQUARE AND CUBE OF NUMBER
AIM
To write a shell program to find the square and cube for the first five numbers.
PROGRAM
OUTPUT
$ Sh sqcube
RESULT
Thus the above shell program to the square and cube for the first five numbers is
been verified and executed successfully.
18
2.7. SUM OF N NATURAL NUMBERS
AIM
PROGRAM
OUTPUT
$ Sh nna
RESULT
Thus the above shell program to find the sum of ‘N’ natural Numbers is been
verified and executed successfully.
19
2.8. SUM OF DIGITS IN GIVEN NUMBER
AIM
To write a shell program to find the sum of digits in the given number.
PROGRAM
OUTPUT
$ Sh sd
RESULT
Thus the above shell program to find the sum of digits in the given number is
been verified and executed successfully.
20
C PROGRAMMING
ON UNIX
21
3.1. LINEAR SEARCH
AIM:
ALGORITHM:
22
PROGRAM
#include<stdio.h>
23
OUTPUT:
RESULT
Thus the above C program to perform linear search using function is been
verified and executed successfully.
24
3. 2. ARRAY USING POINTERS
AIM
ALGORITHM
25
PROGRAM
#include<stdio.h>
void main()
int *a[10],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("\n a[%d]=%d",i,*(a+i));
26
OUTPUT:
a[0]=10
a[1]=20
a[2]=30
a[3]=45
RESULT
Thus the above C program to print the elements of an array string manipulation
using pointers is been verified and executed successfully.
27
3.3. LENGTH OF STRING
AIM:
ALGORITHM:
Step 2: Declare the character array and assign a string char s1[]= ”anand”
28
PROGRAM
#include<stdio.h>
#include<string.h>
int l(char*);
main()
{
int len;
static char s1[]="anand ";
len=l(s1);
printf("\n The length of String s1 %d\n",len);
}
int l(char*ptr)
{
int s=0;
while(*ptr!='\0')
{s+=1;
ptr++;
}
return(s);
}
29
OUPUT:
RESULT
Thus the above C program to find the length of the string is been verified and
executed successfully.
30
3.4. STRING CONCATENATE
AIM:
To write a C program to concatenate two strings.
ALGORITHM:
Step 3. Allocate memory for s1,s2 and s3 character pointer variables using
mallloc function
31
PROGRAM
#include<stdio.h>
#include<malloc.h>
#define length 30
#include<conio.h>
main()
{
char *s1,*s2,*s3,c;
int i,k;
//clrscr();
s1=(char *)malloc(length *sizeof(char));
s2=(char *)malloc(length *sizeof(char));
s3=(char *)malloc(2*length *sizeof(char));
printf("\n Enter the String S1\n");
scanf("%s",s1);
printf("\n Enter the String S2\n");
scanf("%s",s2);
i=0;
while((c=*(s1+i))!='\0')
{
s3[i]=c;
i++;
}
k=0;
while((c=*(s2+k))!='\0')
{
s3[i+k]=c;
k++;
}
s3[i+k]='\0';
printf("\n the concatenated string is %s",s3);
printf("\n");
}
32
Output:
RESULT
Thus the above C program to concatenate two strings is been verified and
executed successfully.
33
3.5. FILE HANDLING
AIM:
ALGORITHM:
Step 5. Open the file fname1 under read mode assigned to file pointer-fp1.
Step 6. Open the file fname2 under write mode assigned to file pointer-fp2.
Step 8. Print file fname1 cannot be opened for reading and then goto step16
Step10. Print file fname2 cannot be opened for writing and then goto
step16
34
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp1,*fp2;
int c;
char fname1[40],fname2[40];
clrscr();
printf("\n Enter the source file\n");
gets(fname1);
printf("\n Enter the target file\n");
gets(fname2);
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"w");
if(fp1==NULL)
{
printf("\n Cannot open %s for reading \n",fname1);
getch();
exit(1);
}
else if(fp2==NULL)
{
printf("\n Cannot open %s for writing \n",fname2);
getch();
exit(1);
}else
{
c=getc(fp1);
while(c!=EOF)
{
putc(c,fp2);
c=getc(fp1);
}
fclose(fp1);
fclose(fp2);
printf("\n File successfully copied\n");
}
getch();
}
35
Output:
RESULT
Thus the above C program to copy the content of one file to another is been
verified and executed successfully.
36