You are on page 1of 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 don’t 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.

Beginning of the line. Last character of the word. 2) Insert 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. Some More Cursor Movements: Moves forward by word. -Works the same way as “/” -Used when we want to specify a command. . -Return the vi editor to the command mode.Format : $echo “Message” Example: $echo “UNIX” UNIX 7) read Command: This command is used in shell script. -Helps to search for a particular string with in the file. End of the line. Moves backward by word. 3) Command mode. 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.

change to insert mode. to delete the second line. Allows insertion by creating a blank line above the current line. set the line number.3. second line is copied after seventh line. Deleting data: :u :set number :2Jo :2co7 :1. It overwrites from current cursor position. used for appending the text.3co8 :3mo7 :2d :2. Text is appended at back end of the line. moves forward direction by screen. Esc mode-deleting datas: . move third line after seventh line.(2.L - Last line of the file. to delete the set of lines. moves the cursor half of the screen in forward direction. used to join operation. quit after writing the changes to the disk file. first. delete the single word. invokes insert mode and insertion takes place at the beginning.third lines are copied after eighth line.4&5) How to quit vi editor: quit without writing the changes into disk file. delete the line in which cursor position. Used for appending the text. 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. moves the cursor half of the screen in backward direction.second.5d :q! :wq dd dw undo all commands used in command prompt. moves backward direction of the screen. Text is appended after the cursor position. Allows insertion by creating a blank line below the current line.

delete the single character before the cursor position. RESULT: Thus the basic UNIX commands were studied and executed Ex.x/del X - to delete the single character at the cursor position.No: 2 SHELL PROGRAMMING .

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

Choice 2) command... do command done The while Construct: Syntax: while control command do command list done .value2……….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. Esac The for command: Syntax: for<variable>in value1.

Syntax: sb. 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. Ex.No: 03 ARITHMETIC OPERATION USING SHELL PROGRAM AIM: .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.x filename.

ALGORITHM: Step 1: Start Step 2: Read the values of ‘a’ and ‘b’ Step 3: Write the expression to calculate sum. Subtraction. Division.: 04 GREATEST OF THREE NUMBERS AIM: To write a shell program to find the greatest of three Numbers.To write a shell Program to do the Arithmetic Operations like Addition. SHELL PROGRAM: echo "Enter Two Numbers" read a b c=`expr $a + $b` d=`expr $a . Multiplication. Step 4: Print the Results Step 5: Stop.$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. Product. Quotient and Remainder. difference. . No. Thus the shell Program to do arithmetic operation was executed without any errors.

: 05 CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT . ‘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. No. Ex.ALGORITHM: Step 1: Start Step 2: Read the value of ‘a’. 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.

z=y*y*y. 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: . 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. sum=num+z. Step 4: When the value of ‘num’ is greater then 0. solve the following expressions Step 5: y = num/10.AIM: To check whether the given Number is Armstrong Number or not using Shell Programming. 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.

.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.

: 06 FIBONACCI SERIES AIM: To write a Shell Program to print the Fibonacci series. b=1. 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 . ALGORITHM: Step 1: Start Step 2: Read the Value of ‘n’ Step 3: Initialize i=2. 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. Step 4: Print the Value of ‘a’ and ‘b’ Step 5: When ‘i’ is lesser than ‘n’. a=0.Ex No.

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. .

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.

.$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. 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 – b.$b` a=`expr $a . a=a + b. 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.

No. Step 10: Stop. using Shell Programming.Ex. Step 5: If the value of ‘n’ is not equal to 0.: 08 NUMBER OF DIGITS IN AN INTEGER AIM: To count the number of digits in an integer. 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. 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" . solve n=a %10. 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.

.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.

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`".. 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 . Step 3: Get the operation choice from the User Step 4: Give the expressions for each case and solve them.Ex.. esac . 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. No...$b`".

.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.SAMPLE OUTPUT: [root@localhost Shell]# sh arith_switch.

SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n .1` done echo "The Factorial of the given Number is $n" SAMPLE OUTPUT: [root@localhost Shell]# sh fact. 10 FACTORIAL OF A NUMBER AIM: To write a Shell Program to find the factorial of a Number. Step 7: Stop. No. . 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.Ex.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.1` p=1 while [ $i -ge 1 ] do n=`expr $n \* $i` i=`expr $i .

perform the following steps.1` done . 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. No.Ex.: 11 CHECKING FOR PRIME NUMBER AIM: To write a Shell Program to check if the Number is a Prime Number or a Composite Number.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 . print “The Number is a Prime Number” SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n . 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.

.sh Enter a Number 2 The Number 2 is a Prime Number [root@localhost Shell]# sh prime.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. was executed and the output was verified.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.

ALGORITHM: Step 1: Start Step 2: Read the Integer as ‘num’ Step 3: Initialize the value of sum=0. 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" . No. 12 SUMS OF DIGITS OF AN INTEGER AIM: To write a Shell Program to calculate the sum of digits of an Integer. solve the following expressions. Step 4: While the value of ‘num’ is greater than 0.Ex.

.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.

printf("Enter the 2nd Matrix: \n\n").j++) scanf("%d".C PROGRAMMING UNDER UNIX ENVIRONMENT Ex. scanf("%d%d".i++) for(j=0. n.j<n. 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].i<m.j. printf("Enter the 1st Matrix: \n\n").i<m. for(i=0.i<m. printf("\nEnter the Number of Rows and Columns:\n\n ").b[10][10]. 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. for(i=0. C PROGRAM: #include<stdio.j++) scanf("%d". for(i=0. No.i++) .m. i.j<n. b[10][10].a[10][10].&n).&a[i][j]).i++) for(j=0. j.i.&b[i][j]).n. add[10][10]. m.&m.h> main() { int add[10][10].

for(i=0./a.j<n. } printf("The Addition of the two matrices is: \n").{ for(j=0.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.add[i][j]). for(j=0.i<n.j<n.j++) add[i][j]=a[i][j]+b[i][j]. . } } } SAMPLE OUTPUT: [root@localhost C_Program]# cc matadd.c [root@localhost C_Program]# .j++) { printf("%d\t".i++) { printf("\n\n").

j.&n).i++) scanf("%d".h> main() { int x[50].i<n-1. 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. Step 8: Stop C PROGRAM: #include<stdio.j++) if(x[i]>x[j]) { temp=x[i]. No.Ex.n. x[i]=x[j]. for(i=0. for(i=0.i++) for(j=i+1. 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’. } printf("\nThe Sorted list of Numbers is \n"). 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. printf("\nEnter the List of %d numbers:\n".temp.i.n). printf("How many Numbers ?\n").j<n.i<n. scanf("%d".&x[i]). x[j]=temp. .

} SAMPLE OUTPUT: [root@localhost C_Program]# cc sort.x[i]). .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.i<n.i++) printf("%d\t".for(i=0.

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

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

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

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

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

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.Count 5. .1.Change 4.Read the file 2.Copy the file 3.

i<n. for(i=0.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.i++) { for(j=i+1.n.&n). get the array of integers to the value of ‘n’ Step 4: Again using ‘for’ loop.Ex.j<n. . ALGORITHM: Step 1: Start Step 2: Get the maximum limit for the array as ‘n’ Step 3: Using ‘for’ loop. Step 7: Print the result Step 8: Stop PROGRAM: #include<stdio.h> main() { int a[100]. printf("\nEnter %d numbers: \n".n). a[i]=a[j].j++) { if(a[i]>a[j]) { temp=a[i]. Step 6: Swap the value of a[0] to ‘temp’.temp. a[j] to a[i] and a[i] to ‘temp’.j. sole the following conditions. scanf("%d". Step 5: If the first digit is greater than the second.i.i++) scanf("%d". for(i=0.&a[i]). printf("\nEnter the maximum limit: ").i<n. perform the following swap conditions.

/a.c [root@localhost C_Program]# .a[0]). .a[n-1]).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.a[j]=temp. } SAMPLE OUTPUT: [root@localhost C_Program]# cc maxmin. printf("\nThe biggest number is %d". } } } printf("\nThe smallest number is %d".

n. solve the following expressions. while(n>=1) { a=n%10. b=(b*10)+a. } if(c==b) printf("\nThe number is a palindrome\n").c. Step 4: While the value of ‘n’ is greater than or equal to 1.b=0. No. n=n/10. Step 8: Stop.h> main() { int a. print “The number is a palindrome”. Step 7: If the above condition is not satisfied. printf("\nEnter a number: ").: 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’. Solve b=(b*10)+a. PROGRAM: #include<stdio. and n=n/10 Step 6: If the value of ‘c’ is equal to ‘b’. scanf("%d". Step 5: Store the remainder of ‘n’ by 10 as ‘a’.Ex.&n). c=n. print “The number is not a palindrome”. else .

printf("\nThe number is not a palindrome\n").out Enter a number: 12321 The number is a palindrome [root@localhost C_Program]# ./a. } SAMPLE OUTPUT: [root@localhost C_Program]# cc palin./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. .c [root@localhost C_Program]# .

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

for(i=0.i++) { printf("%c".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.i<100. .out My name is Barnabas t? <@PB@.buf.c [root@localhost C_Program]# .read(fd.100).buf[i]). } } SAMPLE OUTPUT: [root@localhost C_Program]# cc syscall-r./a.

h  File control Options unistd. . open the file ‘aa’ to find the garbage values stored in it. Step 3: Get the data form the user and store it as ‘buf’.Ex.fd. printf("Give data: ").buf). scanf("%s". Step 4: Open the file ‘aa’ as write-only and store it in fd. fd=open("aa". ALGORITHM: Step 1: Start Step 2: Initialize the integer variables – ‘i’ and ‘fd’ with a character variable ‘buf[100]’.h> #include<unistd.h> main() { int i. A file ‘aa’ with data must be created before the program is compiled and is run.h  Standard symbolic constants and types.h> #include<fcntl. No. Step 5: Write the data given by the user to the file and ‘fd’ Step 6: Stop Note: After the program is run. to avoid segmentation error.: 20 SYSTEM CALL FOR WRITE-ONLY FILE AIM: To write a C Program to perform system call to open a write-only file. Header Files: fcntl. PROGRAM: #include<stdio. char buf[100].O_WRONLY).

. } SAMPLE OUTPUT: [root@localhost C_Program]# cc syscall-w.buf.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.write(fd.c [root@localhost C_Program]# .sizeof(buf)).out Give data: I study in VEC [root@localhost C_Program]# cat aa IBe? B B? Bt? <@PB@./a.

and store it in fp1 Step 3: Open a new file. Step 4: Copy all the characters one by one until the End of File. while((ch=getc(fp1))!=EOF) { putc(ch. fp1=fopen(av[1].c [root@localhost C_Program]# cat>a Velammal Engineering College [root@localhost C_Program]# ./a."w"). char *av[2]) { char ch.h> main(int a. } fclose(fp1).*fp2.fp2). } SMAPLE OUTPUT: [root@localhost C_Program]# cc copy. C PROGRAM: #include<stdio.: 21 IMPLEMENTATION OF COPY COMMAND AIM: To write a C Program to implement copy command in UNIX.out a b . No."r"). where the contents are to be copied and store it in fp2. fclose(fp2). FILE *fp1.Ex. ALGORITHM: Step 1: Start Step 2: Open the file to be copied. Step 6: Stop. Step 5: Close all the files. fp2=fopen(av[2].

[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 .

Step 5: Stop. Step 4: Using string compare function."r"). c=c++. print the result with respect to the command given.c=0.Ex. Step 3: Using If – Else condition count the no. if(strcmp(av[1]. C PROGRAM: #include<stdio."r"). of characters. if(a==2) f=fopen(av[1]. while(!feof(f)) { ch=getc(f)."-c")==0) .l=0. else f=fopen(av[2].char *av[3]) { FILE *f. letters and words. l=0. int w=0.: 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. } fclose(f).h> main(int a. No. if(ch==' '||ch=='\n') w=w+1. w=0. if(ch=='\n') l=l+1. char ch.

w). of characters are %d\n"./a./a.c-1.out -w msb The no.c [root@localhost C_Program]# . of words are %d\n".w. } SAMPLE OUTPUT: [root@localhost C_Program]# cc wc."-w")==0) printf("\nThe no. else if(strcmp(av[1]."-l")==0) printf("\nThe number of lines are %d\n".l).out -c msb The no. else if(strcmp(av[1].printf("\nThe no.out msb 77 10 7 [root@localhost C_Program]# ./a.c-1). ./a. of words are 10 [root@localhost C_Program]# . of characters are 77 RESULT: Thus the C Program to implement wc commad in UNIX was executed and the output was verified.out -l msb The number of lines are 7 [root@localhost C_Program]# . else if(av[2]=='\0') printf("\n%d\t%d\t%d\n".l).

if(f==0) { printf("\nChild Process ID: %d\n". } else { printf("\nProcess ID: %d\n". print the Child process ID. C PROGRAM: #include<stdio. } } SAMPLE OUTPUT: [root@localhost C_Program]# cc fork. Step 4: Stop.getppid()).c [root@localhost C_Program]# .h> main() { int f. No.getppid()). printf("\nParent ID: %d\n". f=fork(). unistd. Parent ID and the process ID./a.getpid()). printf("\nParent ID: %d\n".: 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.Ex.out Child Process ID: 3844 .getpid()).h> #include<unistd.h and store it in f Step 3: When f is equal to 0. using inbuilt ID functions.

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. .

c [root@localhost C_Program]# . Step 3: Stop C PROGRAM: #include<stdio.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.h> #include<unistd.h> main() { execl("/bin/date".Ex. .: 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. } SAMPLE OUTPUT: [root@localhost C_Program]# cc execl."date". to print the Date and Time. No./a.0).