Вы находитесь на странице: 1из 54

185253COMPUTER PRACTICE LABORATORY II LIST OF EXPERIMENTS 1. UNIX COMMANDS Study of Unix OS - Basic Shell Commands - Unix Editor 2.

. SHELL PROGRAMMING

0 1 2 2

15

15

Simple Shell program - Conditional Statements - Testing and Loops 3. C PROGRAMMING ON UNIX Dynamic Storage Allocation-Pointers-Functions-File Handling TOTAL : 45 PERIODS 15

VELAMMAL ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING LAB COURSE PLAN DEPT: ECE C SUB: COMPUTER PRACTICE LAB II S.NO 1 2 3 4 5 6 7 8 9 10 11 12 NAME OF THE EXPERIMENT Study of Unix OS Basic Shell Commands I Basic Shell Commands - II Simple Shell program Conditional Statements Testing and Loops Model Lab - I Dynamic Storage Allocation Pointers - I Functions File Handling Model Lab - II BATCH-A DATE SEMESTER: II SEM SUB CODE: 185253 PERIOD BATCH-B DATE PERIOD

Prepared by Signature Name Designation Date R.TAMILARASI Assistant Professor - III

Approved By

Dr.N.SANKAR RAM HOD/CSE

Ex.No:1

UNIX COMMANDS

Introduction: The UNIX operating system developed at Bell laboratories are among the notable success in the field of operating system. UNIX system provides a friendly environment for program development and text processing. They make it easy to combine programs with one another. This encourages a modular total oriented building block approach to program design. BASIC UNIX COMMANDS 1. date command: The date command tells us the current date and time Format: $date 2. who command: This command gives us the details of who have logged into the Unix System. Format: $who $who am i which tells us to when we had logged in and the system name for connection being used 3. man command: This command prints manual page Format: $man<command name> Example: $man who Usage of Directory commands: 1. pwd command: It tells us the full path from the current directory Format: $pwd

2. ls command: The ls command displays the list of files in a current directory Format: $ls The ls command comes with different options. Some of them are given here, ls-l ls-t ls-a ls-b ls-p ls-u List files in login format. The files are displayed along with there mode, number of links, owner of files, file size, modification date and time of file name List in order of last modification time(most recent file) List all entries, including hidden files List directory files instead of contents Put a slash after each directory List in order of last access time

3. mkdir command : This command helps us to make a directory Format: $mkdir <directory name> Example: mkdir cape 4. cd Command: This command helps us to change from working directory to any other directory specified Example: Assume that you are working in the directory path/user/you $mkdir book $cd book $pwd /user/you/book $cd.. $cd.. $pwd /user/you $cd 5. rmdir Command: This helps to return to home directory. This is used to move up one level in file system. This makes a new directory Used to change the directory Go to it

This command is used to remove a directory specified in the command . Format: $rmdir<directory name> Example: $rmdir book Usage of file command: 1) cat command: This command helps us to count the content of the file we specify. If we dont specify the name of the file ,it takes input from standard input. Format : $cat [path] filename Example: $cat xyz Example: $cat cape1 Have a nice day Ctrl+l 2) cp Command: This command helps us to create the copies of a ordinary file. Format : $cp[path]sourcefilename[path]destinatiosnfilename Example: $cp cape1 cape2 3) ln Command: This command is used to establish a additional filename for the same directory file. Format : $ln oldfilename new filename Example: $ln cape1 cape2 4) mv Command: This command is used to move the contents from one file to another file. Format : $mv Source Destination Example: $mv cape1 cape2 5) rm Command: This command is used to remove one or more files from a directory. This can be used to delete all file as well as the directory. Format: $rm [path] file 6) echo Command: This command is used to display on the screen.

Format : $echo Message Example: $echo UNIX UNIX 7) read Command: This command is used in shell script. This command waits for the user to in pact the value of the variable. Format : $read variable name Important Keys to Remember: Enter Key Esc Key / ? : Three modes: 1) Esc mode. 2) Insert mode. 3) Command mode. Esc mode: Movement of the Cursor: h l k j w b e ^ $ or or or or backspace:left spacebar :right + :up :down -Execute command in the escape model & start a new line in the insert mode. -Return the vi editor to the command mode. -Helps to search for a particular string with in the file. -Works the same way as / -Used when we want to specify a command.

Some More Cursor Movements: Moves forward by word. Moves backward by word. Last character of the word. Beginning of the line. End of the line.

Last line of the file. moves forward direction by screen. moves backward direction of the screen. moves the cursor half of the screen in backward direction. moves the cursor half of the screen in forward direction. change to insert mode. invokes insert mode and insertion takes place at the beginning. used for appending the text. Text is appended after the cursor position. Used for appending the text. Text is appended at back end of the line. Allows insertion by creating a blank line above the current line. Allows insertion by creating a blank line below the current line. It overwrites from current cursor position.

Screen Commands: Ctrl+F Ctrl+B Ctrl+D Ctrl+U i/I I a A O o R Insert mode:

Command mode: Esc+: -change to command mode. Deleting data: :u :set number :2Jo :2co7 :1,3co8 :3mo7 :2d :2,5d :q! :wq dd dw undo all commands used in command prompt. set the line number. used to join operation. second line is copied after seventh line. first,second,third lines are copied after eighth line. move third line after seventh line. to delete the second line. to delete the set of lines.(2,3,4&5)

How to quit vi editor: quit without writing the changes into disk file. quit after writing the changes to the disk file. delete the line in which cursor position. delete the single word.

Esc mode-deleting datas:

x/del X

to delete the single character at the cursor position. delete the single character before the cursor position.

RESULT: Thus the basic UNIX commands were studied and executed

Ex.No: 2

SHELL PROGRAMMING

Introduction: The shell is the unix system mechanism for interfacing between user and the system.It is command interpreter that reads line typed by the user and causes the execution of the requested system features. Three of the most popular shells are Bourne shell,berkely$shell and kern shell. The Bourne shell has been the primary at Unix system shell.Shell command language includes control flow capabilities command language program may be written using if then..else,case..while for structure. These programs are written in interfacing editors like vi editors. Shell programming is a series of unix command executed one by one. 1) The #Symbol: This symbol is used in shell script to reverse the value that is scanned. 2) The $Symbol: This command is used in shell script to reverse the value that is scanned. 3) The read Command: This command is used in shell script to reverse the value of the variable. Syntax: read Variable name 4) The echo command: This command is used to display what ever message we want to display on the screen. Arithmetic Operation & Expression: The arithmetic operators provided by the shell are Addition in operator, Subtraction in operators, Multiplication & Division/operator. The expr command helps us in getting the numerical value of the digit strings. Logical Operators: a o Used for indicating AND Used for indicating OR will check if the integer are equal will check if the two integer are not equal

Comparison Operators: n1-eq n2 n1-ne n2

n1-gt n2 n1-lt n2 n1-ge n2 n1-le n2

will check if the integer n1 is greater than n2 will check if the integer n1 is less than n2 will check if the integer n1 is greater than or equal to n2 will check if the integer n1 is less than or equal to n2

Programming Language Constructs: The if construct: Syntax 1 If(condition) then command else command fi The case Construct: Syntax: case value n Choice 1) commands;; Choice 2) command;; Esac The for command: Syntax: for<variable>in value1,value2. do command done The while Construct: Syntax: while control command do command list done

The until loop: Syntax: until control command do command done The execution Commands: A shell script can be executed in two ways 1) Type in sb FILENAME at the $ prompt. 2) Grant execute permission to the file and then type in the FILENAME at the $prompt The Debug Command: This command is used for debugging the program. Syntax: sb- x filename.

Ex.No: 03 ARITHMETIC OPERATION USING SHELL PROGRAM AIM:

To write a shell Program to do the Arithmetic Operations like Addition, Subtraction, Multiplication, Division. ALGORITHM: Step 1: Start Step 2: Read the values of a and b Step 3: Write the expression to calculate sum, difference, Product, Quotient and Remainder. Step 4: Print the Results Step 5: Stop. SHELL PROGRAM: echo "Enter Two Numbers" read a b c=`expr $a + $b` d=`expr $a - $b` e=`expr $a \* $b` f=`expr $a / $b` g=`expr $a % $b` echo "Sum = $c" echo "Difference = $d" echo "Product = $e" echo "Quotient = $f" echo "Remainder = $g" SAMPLE OUTPUT: Enter Two Numbers 12 10 Sum = 22 Difference = 2 Product = 120 Quotient = 1 Remainder = 2 RESULT: Ex. No.: 04 GREATEST OF THREE NUMBERS AIM: To write a shell program to find the greatest of three Numbers. Thus the shell Program to do arithmetic operation was executed without any errors.

ALGORITHM: Step 1: Start Step 2: Read the value of a, b and c Step 3: Check if a is greater than b and a is greater than c Step 4: Then print A is Greater Step 5: If Step 3 is not satisfied check if b is greater than c Step 6: Then print B is Greater Step 7: Else Print C is greater Step 8: Stop. SHELL PROGRAM: echo "Enter Three Numbers" read a b c if [ $a -gt $b -a $a -gt $c ] then echo "$a is Greater" elif [ $b -gt $c ] then echo "$b is Greater" else echo "$c is Greater" fi SAMPLE OUTPUT: Enter Three Numbers 12 14 16 16 is Greater RESULT: Thus the program to find the greatest of three Numbers was executed and the output was verified.

Ex. No.: 05 CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT

AIM: To check whether the given Number is Armstrong Number or not using Shell Programming. ALGORITHM: Step 1: Start Step 2: Read the value of num Step 3: Store the value of num in x and initialize the value of sum = 0. Step 4: When the value of num is greater then 0, solve the following expressions Step 5: y = num/10; z=y*y*y; sum=num+z; num=num/10 Step 6: Close the While Loop Step 7: Check if x is equal to sum Step 8: Print the Result Step 9: Stop. SHELL PROGRAM: echo "Enter a Number" read num x=$num sum=0 while [ $num -gt 0 ] do y=`expr $num % 10` z=`expr $y \* $y \* $y` sum=`expr $sum + $z` num=`expr $num / 10` done if [ $x -eq $sum ] then echo "$x is an armstrong Number" else echo "$x is not an armstrong Number" fi SAMPLE OUTPUT:

Enter a Number 153 153 is an armstrong Number [root@localhost root]# sh arm.sh Enter a Number 123 123 is not an armstrong Number

RESULT: Thus the Shell Program to check whether the given Number is Armstrong or not was executed and the output was verified.

Ex No.: 06 FIBONACCI SERIES

AIM: To write a Shell Program to print the Fibonacci series. ALGORITHM: Step 1: Start Step 2: Read the Value of n Step 3: Initialize i=2; a=0; b=1. Step 4: Print the Value of a and b Step 5: When i is lesser than n, solve the expression c=a+b Step 6: Print c Step 7: Swap the Values of b to a and c to b Step 8: Solve the expression i=i+1 Step 9: Stop. SHELL PROGRAM: echo "Enter the Limit" read n i=2 echo "Fibonacci Series" echo "----------------" a=0 b=1 echo $a echo $b while [ $i -lt $n ] do c=`expr $a + $b` echo $c a=$b b=$c

i=`expr $i + 1` done SAMPLE OUTPUT: Enter the Limit 8 Fibonacci Series ---------------0 1 1 2 3 5 8 13

RESULT: Thus the Shell Program to print the Fibonacci Series was executed and the output was verified.

Ex. No.: 07(a) SWAPPING OF VALUES (Using 3rd Variable) AIM: To write a Shell Program to swap two values using 3rd variable. ALGORITHM: Step 1: Read the Values of a and b Step 2: Swap the values of a to the third variable temp Step 3: Swap the value of b to a and b to temp Step 4: Print the swapped values of a and b Step 5: Stop. SHELL PROGRAM: echo "Enter Two Numbers" read a b temp=$a a=$b b=$temp echo $a $b SAMPLE OUTPUT: Enter Two Numbers 12 10 10 12

RESULT: Thus the program to swap two values using 3rd variable was executed and the output was verified.

Ex. No.: 07(b) SWAPPING OF VALUES (Without using 3rd Variable) AIM: To write a Shell Program to swap two values without using 3rd Variable. ALGORITHM: Step 1: Start Step 2: Read the values of a and b Step 3: Solve the expression, a=a + b; b=a b; a=a b Step 4: Print the swapped values of a and b Step 5: Stop SHELL PROGRAM: echo "Enter Two Numbers" read a b a=`expr $a + $b` b=`expr $a - $b` a=`expr $a - $b` echo $a $b SAMPLE OUTPUT: Enter Two Numbers 12 10 10 12

RESULT: Thus the Shell Program to swap two values without using third variable was executed and the output was verified.

Ex. No.: 08 NUMBER OF DIGITS IN AN INTEGER AIM: To count the number of digits in an integer, using Shell Programming. ALGORITHM: Step 1: Start Step 2: Read the value of a Step 3: Initialize the value of c as 0 Step 4: When the value of a is not equal to 0, solve n=a %10. Step 5: If the value of n is not equal to 0, solve c=c + 1(increment) Step 6: End if condition Step 7: Solve the expression a=a / 10 Step 8: Repeat steps 4 to 7 until a is equal to 0 Step 9: Print the value of c as the number of digits in an integer. Step 10: Stop. SHELL PROGRAM: echo "Enter a Number" read a c=0 while [ $a -ne 0 ] do n=`expr $a % 10` if [ $n -ne 0 ] then c=`expr $c + 1` fi a=`expr $a / 10` done echo "The Number of Digits in the Integer is $c"

SAMPLE OUTPUT: Enter a Number 123456789 The Number of Digits in the Integer is 9

RESULT: Thus the Shell Program to calculate the number of digits of an integer was executed and the output was verified.

Ex. No. 09 ARITHMETIC OPERATIONS USING SHELL PROGRAMMING (Using SWITCH - CASE) AIM: To write a Shell Program to perform Arithmetic Operations using Switch Case ALGORITHM: Step 1: Start Step 2: Read the two Numbers. Step 3: Get the operation choice from the User Step 4: Give the expressions for each case and solve them. Step 5: Print the Result Step 6: Stop

SHELL PROGRAM: echo "Enter Two Numbers" read a b echo "What do you want to do? (1 to 5)" echo "1) Sum" echo "2) Difference" echo "3) Product" echo "4) Quotient" echo "5) Remainder" echo "Enter your Choice" read n case "$n" in 1) echo "The Sum of $a and $b is `expr $a + $b`";; 2) echo "The Difference between $a and $b is `expr $a - $b`";; 3) echo "The Product of the $a and $b is `expr $a \* $b`";; 4) echo "The Quotient of $a by $b is `expr $a / $b`";; 5) echo "The Remainder of $a by $b is `expr $a % $b`";; esac

SAMPLE OUTPUT: [root@localhost Shell]# sh arith_switch.sh Enter Two Numbers 12 10 What do you want to do? (1 to 5) 1) Sum 2) Difference 3) Product 4) Quotient 5) Remainder Enter your Choice 4 The Quotient of 12 by 10 is 1

RESULT: Thus the Shell program to perform arithmetic operations using Switch Case was executed and the output was verified.

Ex. No. 10 FACTORIAL OF A NUMBER AIM: To write a Shell Program to find the factorial of a Number. ALGORITHM: Step 1: Start Step 2: Read the Number as n Step 3: Assign the value of p as 0 and solve the expression i = n-1 Step 4: While the value of i is greater than or equal to 1 do the following steps. Step 5: Solve the expression n=n*1 and i=i-1 (Decrementing Operation) Step 6: Then print the value of n as the factorial of the given number. Step 7: Stop. SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n - 1` p=1 while [ $i -ge 1 ] do n=`expr $n \* $i` i=`expr $i - 1` done echo "The Factorial of the given Number is $n" SAMPLE OUTPUT: [root@localhost Shell]# sh fact.sh Enter a Number 5 The Factorial of the given Number is 120 RESULT: Thus the Shell Program to find the factorial of the Number was executed and the output was verified.

Ex. No.: 11 CHECKING FOR PRIME NUMBER AIM: To write a Shell Program to check if the Number is a Prime Number or a Composite Number. ALGORITHM: Step 1: Start Step 2: Read the Number as n Step 3: Initialize the value of t to 0 and solve the expression i=n-1 Step 4: While i is greater than or equal to 2, perform the following steps. Step 5: Solve p=n%i Step 6: Check the condition (p=0) and then solve t=t+1 Step 7: End the If Condition Step 8: Solve the expression i=i-1 Step 9: If t is greater than 0, print The Number is not a Prime Number Step 10: If the condition is not satisfied, print The Number is a Prime Number SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n - 1` t=0 while [ $i -ge 2 ] do p=`expr $n % $i` if [ $p -eq 0 ] then t=`expr $t + 1` fi i=`expr $i - 1` done

if [ $t -gt 0 ] then echo "The Number $n is not a Prime Number" else echo "The Number $n is a Prime Number" fi SAMPLE OUTPUT: [root@localhost Shell]# sh prime.sh Enter a Number 2 The Number 2 is a Prime Number [root@localhost Shell]# sh prime.sh Enter a Number 4 The Number 4 is not a Prime Number

RESULT: Thus the Shell Program to check if the given number is prime or not, was executed and the output was verified.

Ex. No. 12 SUMS OF DIGITS OF AN INTEGER AIM: To write a Shell Program to calculate the sum of digits of an Integer. ALGORITHM: Step 1: Start Step 2: Read the Integer as num Step 3: Initialize the value of sum=0. Step 4: While the value of num is greater than 0, solve the following expressions. Step 5: Find the remainder of num by 10 and store in y Step 6: Add sum with y and store it in sum Step 7: Divide num by 10 and store the value in num Step 7: Close the while loop Step 8: Print the Result Step 9: Stop

SHELL PROGRAM: echo "Enter a Number" read num sum=0 while [ $num -gt 0 ] do y=`expr $num % 10` sum=`expr $sum + $y` num=`expr $num / 10` done echo "The Sum of the Digits of the Integer is $sum"

SAMPLE OUTPUT: [root@localhost Shell]# sh digits_sum.sh Enter a Number 123456 The Sum of the Digits of the Integer is 21

RESULT: Thus the Shell program to calculate the sum of the digits of the integer was executed and the output was verified.

C PROGRAMMING UNDER UNIX ENVIRONMENT Ex. No. 13 MATRIX ADDITION AIM: To write a C Program to add two matrices under UNIX Environment ALGORITHM: Step 1: Start Step 2: Declare a[10][10], b[10][10], add[10][10], i, j, m, n. Step 3: Get the number of rows and columns as m and n respectively Step 4: Scan the values of the matrices according to the values of m and n. Step 5: Add the two matrices directly and store the values in add[10][10] Step 6: Using for loop print the values of add[10][10] Step 7: End the loop Step 8: Stop.

C PROGRAM: #include<stdio.h> main() { int add[10][10],a[10][10],b[10][10],i,j,m,n; printf("\nEnter the Number of Rows and Columns:\n\n "); scanf("%d%d",&m,&n); printf("Enter the 1st Matrix: \n\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter the 2nd Matrix: \n\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); for(i=0;i<m;i++)

{ for(j=0;j<n;j++) add[i][j]=a[i][j]+b[i][j]; } printf("The Addition of the two matrices is: \n"); for(i=0;i<n;i++) { printf("\n\n"); for(j=0;j<n;j++) { printf("%d\t",add[i][j]); } } } SAMPLE OUTPUT: [root@localhost C_Program]# cc matadd.c [root@localhost C_Program]# ./a.out Enter the Number of Rows and Columns: 2 2 Enter the 1st Matrix: 1 2 3 4 Enter the 2nd Matrix: 1 2 3 4 The Addition of the two matrices is: 2 6 4 8

RESULT: Thus the C Program to add two matrices under UNIX Environment was executed and the output was verified.

Ex. No. 14 SORTING OF INTEGERS AIM: To write a C Program to sort an array of integers under UNIX environment ALGORITHM: Step 1: Start Step 2: Get the number of array integers as n Step 3: Using for loop, get the n number of integers as a[] Step 4: Again using for loop check if the second number is greater than the first Step 5: If the above step is true, swap the value of second to first Step 6: Repeat Steps 4 and 5 for all the integers scanned using for loop Step 7: Again using for loop print the sorted list of values. Step 8: Stop C PROGRAM: #include<stdio.h> main() { int x[50],n,i,j,temp; printf("How many Numbers ?\n"); scanf("%d",&n); printf("\nEnter the List of %d numbers:\n",n); for(i=0;i<n;i++) scanf("%d",&x[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(x[i]>x[j]) { temp=x[i]; x[i]=x[j]; x[j]=temp; } printf("\nThe Sorted list of Numbers is \n");

for(i=0;i<n;i++) printf("%d\t",x[i]); } SAMPLE OUTPUT: [root@localhost C_Program]# cc sort.c [root@localhost C_Program]# ./a.out How many Numbers ? 5 Enter the List of 5 numbers: 4 8 3 6 2 The Sorted list of Numbers is 2 3 4 6 8

RESULT: Thus the C Program to sort the given array of integers under UNIX environment was executed and the output was verified.

Ex. No.: 15 EMPLOYEE DETAILS USING STRUCTURES AIM: To write a C Program to get and print the employee details using Structures under UNIX Platform. ALGORITHM: Step 1: Start Step 2: Initialize the structure with employee no., name and salary. Step 3: Inside the main function, get the number of employees Step 4: Using for loop, scan the employee details Step 5: Again using for loop, print the employee details Step 6: Stop C PROGRAM: #include<stdio.h> struct emp { char name[25]; int salary; int empno; }a[20]; main() { int n,i; printf("\n Enter the Number of Employees: "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n\nEnter the %d Employee details",i+1); printf("\n\n1.Employee No.: "); scanf("%d",&a[i].empno); printf("\n2.Name: "); scanf("%s",&a[i].name); printf("\n3.Salary: "); scanf("%d",&a[i].salary); }

for(i=0;i<n;i++) { printf("\n\nEmployee %d",i+1); printf("\n1.Employee No.: %d",a[i].empno); printf("\n2.Employee Name: %s",a[i].name); printf("\n3.Salary: %d",a[i].salary); } } SAMPLE OUTPUT: [root@localhost C_Program]# cc employ.c [root@localhost C_Program]# ./a.out Enter the Number of Employees: 2 Enter the 1 Employee details 1.Employee No.: 1234 2.Name: Ramesh 3.Salary: 12500 Enter the 2 Employee details 1.Employee No.: 2314 2.Name: Ganesh 3.Salary: 12500 Employee 1 1.Employee No.: 1234 2.Employee Name: Ramesh 3.Salary: 12500 Employee 2 1.Employee No.: 2314 2.Employee Name: Ganesh 3.Salary: 12500 RESULT: Thus the C Program to get and print the employee details using Structures was executed and the output was verified.

Ex. No.: 16 FILE MANIPULATION AIM: 1. 2. 3. 4. To write a C Program to perform following file manipulation: Read a file Copy a file Change the case of the entire file Count the number of words, spaces and lines in a file.

C PROGRAM: #include<stdio.h> #include<string.h> main() { char ch,ct; int choice,tr,letters=0,words=0,spaces=0,lines=0; FILE *fp1,*fp2; do { fp1=fopen("data1.txt","r"); printf("\nEnter u'r Choice\n"); printf("\n1.Read the file\n2.Copy the file\n3.Change\n4.Count\n5.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: while(!feof(fp1)) { ch=getc(fp1); printf("%c",ch); } break; case 2: fp2=fopen("data2.txt","w"); fseek(fp1,0L,SEEK_SET); while(!feof(fp1)) { ct=fgetc(fp1); tr=fputc(ct,fp2); } fclose(fp1); fclose(fp2); fp2=fopen("data2.txt","r");

printf("The Content of the copied file is "); while(!feof(fp2)) { ch=fgetc(fp2); printf("%c",ch); } fclose(fp2); break; case 3: fp2=fopen("data2.txt","r"); while(!feof(fp2)) { ch=getc(fp2); if((ch<91)&&(ch>=65)) { ch=ch+32; printf("%c",ch); } else if((ch>=97)&&(ch<=122)) { ch=ch-32; printf("%c",ch); } else { printf("%c",ch); } } break; case 4: fp2=fopen("data2.txt","r"); while(!feof(fp2)) { ch=fgetc(fp2); if(ch==' ') { spaces++; } else if (ch=='\n') { lines++; } else { letters++; }

} printf("\nTotal lines: %d",lines); printf("\nTotal words: %d",spaces+lines); printf("\nTotal spaces: %d",spaces); printf("\nTotal letters: %d",spaces+lines+words); fclose(fp2); break; } } while(choice!=5); } SAMPLE OUTPUT: Enter u'r Choice 1.Read the file 2.Copy the file 3.Change 4.Count 5.Exit 1 Wisdom better than the Rubies. ? Enter u'r Choice 1.Read the file 2.Copy the file 3.Change 4.Count 5.Exit 2 The Content of the copied file is Wisdom better than the Rubies. ?? Enter u'r Choice 1.Read the file 2.Copy the file 3.Change 4.Count 5.Exit 3 wISDOM BETTER THAN THE rUBIES. ?? Enter u'r Choice

1.Read the file 2.Copy the file 3.Change 4.Count 5.Exit 4 Total lines: 1 Total words: 5 Total spaces: 4 Total letters: 5

RESULT: Thus the FILE MANIPULATION operation was performed and the output was verified.

Ex.No.: 17 GREATEST AND SMALLEST NUMBER IN AN ARRAY AIM: To write a C Program to find the greatest and smallest number in an array of integers. ALGORITHM: Step 1: Start Step 2: Get the maximum limit for the array as n Step 3: Using for loop, get the array of integers to the value of n Step 4: Again using for loop, sole the following conditions. Step 5: If the first digit is greater than the second, perform the following swap conditions. Step 6: Swap the value of a[0] to temp, a[j] to a[i] and a[i] to temp. Step 7: Print the result Step 8: Stop

PROGRAM: #include<stdio.h> main() { int a[100],i,j,n,temp; printf("\nEnter the maximum limit: "); scanf("%d",&n); printf("\nEnter %d numbers: \n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j];

a[j]=temp; } } } printf("\nThe smallest number is %d",a[0]); printf("\nThe biggest number is %d",a[n-1]); } SAMPLE OUTPUT: [root@localhost C_Program]# cc maxmin.c [root@localhost C_Program]# ./a.out Enter the maximum limit: 5 Enter 5 numbers: 9 6 2 10 15 The smallest number is 2 The biggest number is 15 RESULT: Thus the C Program to find the greatest and smallest number in an array was executed and the output was verified.

Ex. No.: 18 TO CHECK WHETHER THE NUMBER IS A PALINDROME OR NOT AIM: To write a C Program to check if the given number is a palindrome or not ALGORITHM: Step 1: Start Step 2: Get the number as n Step 3: Swap the value of n to c. Step 4: While the value of n is greater than or equal to 1, solve the following expressions. Step 5: Store the remainder of n by 10 as a; Solve b=(b*10)+a; and n=n/10 Step 6: If the value of c is equal to b, print The number is a palindrome. Step 7: If the above condition is not satisfied, print The number is not a palindrome. Step 8: Stop.

PROGRAM: #include<stdio.h> main() { int a,c,n,b=0; printf("\nEnter a number: "); scanf("%d",&n); c=n; while(n>=1) { a=n%10; b=(b*10)+a; n=n/10; } if(c==b) printf("\nThe number is a palindrome\n"); else

printf("\nThe number is not a palindrome\n"); } SAMPLE OUTPUT: [root@localhost C_Program]# cc palin.c [root@localhost C_Program]# ./a.out Enter a number: 12321 The number is a palindrome [root@localhost C_Program]# ./a.out Enter a number: 123 The number is not a palindrome

RESULT: Thus the C Program to check whether the number is a palindrome or not was executed and the output was verified.

Ex. No.:19 SYSTEM CALL FOR A READ ONLY FILE AIM: To write C Program to perform system call to open for a read-only file. ALGORITHM: Step 1: Start Step 2: Initialize the integer variables- i and fd. Also initialize the string variable, buf[100]. Step 3: Open the already created file aa as a read only and store its contents in fd. Step 4: Read fd, buf, 100 Step 5: Using for loop, print the character array, buf[i] Step 6: The data already stored in aa along with the garbage value will be printed as output. Step 7: Stop Note: A file aa with data must be created before the program is compiled and is run, to avoid segmentation error. Header Files: fcntl.h File control Options unistd.h Standard symbolic constants and types.

PROGRAM: #include<stdio.h> #include<fcntl.h> #include<unistd.h> main() { int i,fd; char buf[100]; fd=open("aa",O_RDONLY);

read(fd,buf,100); for(i=0;i<100;i++) { printf("%c",buf[i]); } } SAMPLE OUTPUT: [root@localhost C_Program]# cc syscall-r.c [root@localhost C_Program]# ./a.out My name is Barnabas t? <@PB@,X@&8Z@0? @c@TB ? @? B

RESULT: Thus the C Program for system call to open a read-only file was executed and the output was verified.

Ex. No.: 20 SYSTEM CALL FOR WRITE-ONLY FILE AIM: To write a C Program to perform system call to open a write-only file. ALGORITHM: Step 1: Start Step 2: Initialize the integer variables i and fd with a character variable buf[100]. Step 3: Get the data form the user and store it as buf. Step 4: Open the file aa as write-only and store it in fd. Step 5: Write the data given by the user to the file and fd Step 6: Stop Note: After the program is run, open the file aa to find the garbage values stored in it. A file aa with data must be created before the program is compiled and is run, to avoid segmentation error. Header Files: fcntl.h File control Options unistd.h Standard symbolic constants and types.

PROGRAM: #include<stdio.h> #include<fcntl.h> #include<unistd.h> main() { int i,fd; char buf[100]; printf("Give data: "); scanf("%s",buf); fd=open("aa",O_WRONLY);

write(fd,buf,sizeof(buf)); } SAMPLE OUTPUT: [root@localhost C_Program]# cc syscall-w.c [root@localhost C_Program]# ./a.out Give data: I study in VEC [root@localhost C_Program]# cat aa IBe? B B? Bt? <@PB@,X@-8Z@0? @c@TBB ? @?

RESULT: Thus the C Program to perform system call to opena write-only file was executed and the output was verified.

Ex. No.: 21 IMPLEMENTATION OF COPY COMMAND AIM: To write a C Program to implement copy command in UNIX. ALGORITHM: Step 1: Start Step 2: Open the file to be copied, and store it in fp1 Step 3: Open a new file, where the contents are to be copied and store it in fp2. Step 4: Copy all the characters one by one until the End of File. Step 5: Close all the files. Step 6: Stop.

C PROGRAM: #include<stdio.h> main(int a, char *av[2]) { char ch; FILE *fp1,*fp2; fp1=fopen(av[1],"r"); fp2=fopen(av[2],"w"); while((ch=getc(fp1))!=EOF) { putc(ch,fp2); } fclose(fp1); fclose(fp2); } SMAPLE OUTPUT: [root@localhost C_Program]# cc copy.c [root@localhost C_Program]# cat>a Velammal Engineering College [root@localhost C_Program]# ./a.out a b

[root@localhost C_Program]# cat b Velammal Engineering College

RESULT: Thus the C Program to implement copy command in UNIX was executed and the output was verified.

PROGRAMS BEYOND SYLABUS

Ex. No.: 22 IMPLEMENTATION OF WC COMMAND AIM: To write a C Program to implement wc command in UNIX. ALGORITHM: Step 1: Start Step 2: Initialize the variables c=0; w=0; l=0. Step 3: Using If Else condition count the no. of characters, letters and words. Step 4: Using string compare function, print the result with respect to the command given. Step 5: Stop.

C PROGRAM: #include<stdio.h> main(int a,char *av[3]) { FILE *f; char ch; int w=0,l=0,c=0; if(a==2) f=fopen(av[1],"r"); else f=fopen(av[2],"r"); while(!feof(f)) { ch=getc(f); c=c++; if(ch==' '||ch=='\n') w=w+1; if(ch=='\n') l=l+1; } fclose(f); if(strcmp(av[1],"-c")==0)

printf("\nThe no. of characters are %d\n",c-1); else if(strcmp(av[1],"-w")==0) printf("\nThe no. of words are %d\n",w); else if(strcmp(av[1],"-l")==0) printf("\nThe number of lines are %d\n",l); else if(av[2]=='\0') printf("\n%d\t%d\t%d\n",c-1,w,l); }

SAMPLE OUTPUT: [root@localhost C_Program]# cc wc.c [root@localhost C_Program]# ./a.out msb 77 10 7 [root@localhost C_Program]# ./a.out -l msb The number of lines are 7 [root@localhost C_Program]# ./a.out -w msb The no. of words are 10 [root@localhost C_Program]# ./a.out -c msb The no. of characters are 77

RESULT: Thus the C Program to implement wc commad in UNIX was executed and the output was verified.

Ex. No.: 23 FORK SYSTEM CALL AIM: To write a C Program for FORK SYSTEM CALL ALGORITHM: Step 1: Start Step 2: Open the inbuilt function fork() inside the header file, unistd.h and store it in f Step 3: When f is equal to 0, print the Child process ID, Parent ID and the process ID, using inbuilt ID functions. Step 4: Stop.

C PROGRAM: #include<stdio.h> #include<unistd.h> main() { int f; f=fork(); if(f==0) { printf("\nChild Process ID: %d\n",getpid()); printf("\nParent ID: %d\n",getppid()); } else { printf("\nProcess ID: %d\n",getpid()); printf("\nParent ID: %d\n",getppid()); } } SAMPLE OUTPUT: [root@localhost C_Program]# cc fork.c [root@localhost C_Program]# ./a.out Child Process ID: 3844

Parent ID: 3843 Process ID: 3843 Parent ID: 3677

RESULT: Thus the C Program for Fork System Call in UNIX was executed and the output was verified.

Ex. No.: 24 EXECL SYSTEM CALL AIM: To write a C Program for EXECL System Call in UNIX ALGORITHM: Step 1: Start Step 2: Give the command for Execl, to print the Date and Time. Step 3: Stop C PROGRAM: #include<stdio.h> #include<unistd.h> main() { execl("/bin/date","date",0); } SAMPLE OUTPUT: [root@localhost C_Program]# cc execl.c [root@localhost C_Program]# ./a.out Tue May 2 18:01:37 IST 2006 [root@localhost C_Program]# RESULT: Thus the C Program for EXECL System Call in UNIX was executed and the output was verified.

Вам также может понравиться