C Lab Manual r16
C Lab Manual r16
EXERCISE 1(a):
What is OS Command?
Operating system (OS) command line interface are usually distinct programs
supplied with the operating system. A program that implements such a text interface is
often called a command line interpreter, command processes or shell.
Familiarization of Editors-vi, Emacs:
Vi Editor: it is one of the model editors. It has a few modes of operation among which user
must switch. In Linux simply write the code by using Vi Editor. The same keystrokes have
different effects in different modes. The quick reference is below, with the 4 essential
command. VI is a very good editor, but is not an operating environment like emacs. For text
editing, you might like VI.
Syntax: % vi [options] [file ..]
DESCRIPTION
The "vi" text editor is not recommended for newbie’s.
To exit vi (no changes saved) use these five characters: <ESC>:q!<Enter>.
vim: Modern Linux distributions use vim (="vi improved") in place of vi, and vim is
somewhat better than the original vi.
gvim: The GUI version of vi is also available: type gvim in an X terminal.
The most important thing to understand about vi is that is a "modal" editor.
EXAMPLE
% vi sample.c
Emacs : emacs is an editor and an operating environment. To start emacs, issue the
commandEmacs There is a tutorial for emacs that you can run to familiarize yourself with
some of the major features. To start the tutorial, first call up emacs , then issue the
command
Exercise1(b):
Using commands like mkdr , ls, cp,mv,cat,pwd,and man.
man: to see how a command is used on your particular computer. man is the system's
manual viewer; it can be used to display manual pages, scroll up and down, search for
occurrences of specific text, and other useful functions.Each argument given to man is
normally the name of a program, utility or function. The manual page associated with
each of these arguments is then found and displayed. A section number, if provided, will
direct man to look only in that section of the manual. The default action is to search in
all of the available sections, following a pre-defined order and to show only the first page
Syntax % man
mkdir: to make directory in your computer. Is is also use named as (md).
cp: The cp command is used to make copies of files and directories., The command for
copying files is cp. You can include full path names for the files.
Syntax %cp original file new file
Example %cp x.c y.c
Creates a copy of the file in the working directory named origfile. The copy will be
named newfile, and will be located in the working directory.CAREFUL! If the destination
file newfile already exists, it will be overwritten without aconfirmation prompt. This is the
default behavior for all cp operations.If you want to be prompted before overwriting a file,
use the -i (interactive) option. For example:
cp -i oldfile newfile
If you type y (or yes, Y, YES, or any other combination of upper and lowercase of these),
then newfile will be overwritten with a copy of origfile. Typing anything else will abort the
operation.
cp origfile /directory/subdirectory
Creates a copy of the file in the working directory named origfile. The copy will be located in
the directory /directory/subdirectory, and will be named origfile.
cp origfile /directory/subdirectory/.
Same as the above command. The slash-dot ("/.") is implied in the above form of the
command. (The dot is a special file in every Linux directory which means "this directory.")
cp origfile /directory/subdirectory/newfile
Creates a copy of the file in the working directory named origfile. The copy will be
named newfile, and will be located in the directory /directory/subdirectory.
cp file* /directory/subdirectory
Copy every file in the working directory whose name begins with file into the
directory/directory/subdirectory. The asterisk ("*") is a wildcard — a
special character which expands to match other characters. Specifically, the asterisk
wildcard matches zero or more non-whitespace characters. For instance, this command will
copy any files namedfile, file001, file.txt, fileone.jpg, file-archive.zip, etc.
cp file*.jpg /directory/subdirectory
Copy every file in the working directory whose name begins with file, and ends with the file
extension .jpg. For instance, it would make copies of any files
named file,file001.jpg, file002.jpg, or file-new.jpg, etc. The copies will be placed into the
directory /directory/subdirectory.
cp -R /one/two /three/four
Copy the directory two (located in the directory /one), and everything two contains, into
the destination directory /three/four. The result will be called /three/four/two. The
directory /three must already exist for the command to succeed. If the directoryfour does
not already exist in the directory /three, it will be created.
mv: The command to move (rename) a file is mv. Move source file to destination file
Syntax %mv filename1 filename2
Example %mv hello.c bvc.c
Pwd: (%pwd) Short for print working directory, pwd is a Linux, Unix, and FTP command
to print the directory you're currently working in when at the command line.
See the Linux and Unix pwd command page for additional information with this
command.
See the how to use ftp help page for information about pwd and other FTP
commands.
Start
C=(F-32) * 5/9
Print C
F=9*(C/5) +32
Print F
Stop
EXERCISE 2(b):
DESCRIPTION: This program deals with the conversion of temperature given in Fahrenheit
to Celsius and from Celsius to Fahrenheit using basic arithmetic operators like ‘+’ , ’-‘ , ’*’
and ‘/’
ALGORITHM:
Step 1: Start
Step 5: print C
Step 8: print F
Step 9: Stop
SAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
float F,C=0.0;
clrscr();
scanf("%f",&F);
C=(F-32)*5/9;
scanf("%f",&C);
F=9*(C/5)+32;
getch();
OUTPUT:
Temperature in Celsius=10.000000
Temperature in Fahrenheit=50.000000
EXERCISE 3(a):
DESCRIPTION: A leap year is exactly divisible by 4 except for century years (years
ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
Program:
#include<stdio.h>
#include<conio.h>
main()
int year;
printf(“enter a year”);
scanf(“%d”,&year);
if(year%4)==0)
if(year%100==0)
If(year%400==0)
else
else
else
getch();
Output1:
Output2:
EXERCISE 3(b):
DESCRIPTION: This program deals with calculating the sum of digits of a given number by
simply extracting the last digit with n%10 for each and every while loop and making the sum
to each and multiply and every extracted digit until n>0
PROGRAM:
#include<conio.h>
#include<stdio.h>
main()
{
int num,sum=0,r,m=1;
clrscr();
printf("Enter a number: ");
scanf("%d",&num);
while (num)
{
r=num%10;
num=num/10;
sum=sum+r;
m=m*r;
}
printf("Sum of digits of number: %d",sum);
printf(“multiple of a number:%d”,m);
getch();
}
output:
Enter a number: 123
Sum of digits of number: 6
Program:
#include<stdio.h>
#include<conio.h>
main()
int n , i , flag=0;
clrscr();
scanf(“%d”,&n);
for(i=2; i<n/2;i++)
if(n%i==0)
flag=1;
break;
if(flag==0)
printf(“%d is a prime number.”,n);
else
printf(“%d is not a prime number.”,n);
getch();
}
Output:
Read n, x, r,s=0
x=n
n>0
R=r%10,
S=s+pow(r,3)
FLOWCHART:
If(x==s)
stop
C PROGRAMMING LAB MANUAL
Print x
AIM: To write a C program for checking whether the given number is an Armstrong number
or not.
ALGORITHM:
Step 1: start
Step 3: read n
Step 7: if(x==s)then
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,x,r,s=0;
clrscr();
printf("\n Enter a number:\t");
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
scanf("%d",&n);
x=n;
while(n>0)
{
r=n%10;
s=s+pow(r,3);
n=n/10;
}
if(x==s)
printf("\n %d is Armstrong number",x);
else
printf("\n %d is not an armstrong number",x);
getch();
}
OUTPUT: Enter a number: 370
370 is Armstrong number
EXERCISE 4(b):
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,r,k=1;
clrscr();
printf("Enter the range: ");
scanf("%d",&r);
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
getch();
}
Sample output:
Enter the range: 10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
EXERCISE 4(c):
start
Read N
n<-0
If r<-0
n<N
If
r<=n
stop N<-n+1
P<-fact(n)/fact(n-r)*fact(r)
Write p
rr+1
start
If n=0
stop
ALGORITHM:
STEP-1: start
STEP-2: read n
STEP-3: for each n=0 to n<N step n=n+1 repeat the following otherwise go to step 4
STEP-5: for each r=0 to r<n step r=r+1 repeat the following otherwise go to step 9
STEP-7: display p
STEP-8: stop
STEP-9: if n==0
STEP-11: stop
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
PROGRAM:
#include<stdio.h>
#include <conio.h>
main()
{
unsigned int n, r, N;
unsigned long int p;
unsigned long int fact (unsigned int);
clrscr ();
printf ("\n-------PASCAL'S TRIANGLE-------\n");
printf ("\n enter the value of N:\t");
scanf ("%u", &N);
for (n=0;n<N; n++)
{ printf ("\n");
for (r=0; r<=n; r++)
{ p=fact (n)/(fact(n-r)*fact(r));
printf ("%3u", p); }
}
getch ();
}
unsigned long int fact(unsigned int n)
{
if(n==0)
return (1);
else
return (n*fact (n -1));
}
OUTPUT:
Output:
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
a value is:50
b value is:70
B.Call-by-reference:
Aim: to implement call by reference program.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=50,b=70;
clrscr();
swap(&a,&b);
printf(“a value is:%d”,a);
printf(“b value is:%d’,b);
return 0;
getch();
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
EXERCISE 5(b): write a c program illustrating Fibonacci, Factorial with Recursion and without
Recursion.
Fibonacci:
Factorial:
/* ii) factorial of a number (recursive and nonrecursive functions) */
#include <stdio.h>
#include <conio.h>
main()
{
unsigned int n;
unsigned long int nr,r;
unsigned long int fact_rec(unsigned int);
unsigned long int fact_nonrec(unsigned int);
clrscr();
printf("\n------FACTORIAL OF A NUMBER------\n");
printf("\n enter a number (positive integer) \t");
scanf("%u",&n);
nr=fact_nonrec(n);
Output:
------FACTORIAL OF A NUMBER------
Start
Read n1,n2
Read operator
EXERCISE 6(a):
Value=’+’ Res=n1+n2
Value=’-’
Res=n1-n2
Value=’*’ Res=n1*n2
Value=’/’
Res=n1/n2
Value=’%’
Res=n1%n2
Default Display
“Invalid”
Stop
C PROGRAMMING LAB MANUAL
Step 1: Start
Step 5.1: if op= ‘+’ Then compute res=n1+n2 and display res otherwise
go to step 5.2
Step 5.2: if op= ‘-’ Then compute res=n1-n2 and display res otherwise
go to step 5.3
Step 5.3: if op= ‘*’ Then compute res=n1*n2 and display res otherwise
go to step 5.4
Step 5.4: if op= ‘/’ Then compute res=n1/n2 and display res otherwise
go to step 5.6
Step 6: Stop
SAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
int n1,n2,res;
float div;
char op;
clrscr();
scanf("%d%d",&n1,&n2);
fflush(stdin);
fflush(stdin);
op=getchar();
switch(op)
break;
break;
break;
break;
break;
getch();
SAMPLE OUTPUT:
1 2
ADDITION 1 + 2=3
Exercise 6 (b): write a c program to convert decimal to binary and hex (using switch call
function the function)
Program:
#include<stdio.h>
#include<conio.h>
Exercise-7: Write a c program to compute the values of sin x and cosx and e^x values using
series expansion(use factorial function.
Aim: to compute the values of sin x and cosx and e^x values using series expansion(use
factorial function.
Program:
#include<stdio.h>
int main()
{
int choice, x, n;
do
{
printf( "\nMenu\n[1] e^(x)\n[2] Sin(x)\n[3] Cos(x)\n[4] Exit\n" );
scanf( "%d", &choice );
switch ( choice )
{
case 4: // exit
break;
while ( i < n )
{
ex += ( float ) pow( x, i ) / fact( i );
++i;
}
return ex;
}
return value;
}
return value;
}
if ( num == 1 )
return 1;
else
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
f = num * fact( num - 1 );
return f;
}
Output:
Menu
[1] e^(x)
[2] Sin(x)
[3] Cos(x)
[4] Exit
1
e^x
Enter x and n: 2 3
e^2(3)=5.000000
Menu
[1] e^(x)
[2] Sin(x)
[3] Cos(x)
[4] Exit
2
Sin(x)
Enter x and n: 90 2
Sin(90)(2)=0.924843
Menu
[1] e^(x)
[2] Sin(x)
[3] Cos(x)
[4] Exit
3
Cos(x)
Enter x and n: 90 1
Cos(90)(1)=.000000
Menu
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
[1] e^(x)
[2] Sin(x)
[3] Cos(x)
[4] Exit
4
Program:
#include<stdio.h>
main()
{
int a[10],i,n,key,flag=0,pos;
printf("\n enter n value ");
scanf("%d",&n);
printf("\n enter %d array elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter key element to find ");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(key==a[i])
{
flag=1;
pos=i;
break;
}
}
if(flag==1)
Description:
BUBBLE SORT: The bubble sort is also known as the ripple sort. The bubble sort is probably
the first, reasonably complex module that any beginning programmer has to write. It is a
very simple construct which introduces the student to the fundamentals of how sorting
works.
A bubble sort makes use of an array and some sort of "swapping" mechanism. Most
programming languages have a built-in function to swap elements of an array. Even if a
swapping function does not exist, only a couple of extra lines of code are required to store
one array element in a temporary field in order to swap a second element into its place.
Then the first element is moved out of the temporary field and back into the array at the
second element's position.
Here is a simple example of how a bubble sort works: Suppose you have a row of children's
toy blocks with letters on them. They are in random order and you wish to arrange them in
alphabetical order from left to right.
Step 1. Begin with the first block. In this case, the letter G. (Fig. 1.)
Fig. 1
Step 2. Look at the block just to the right of it.
Fig. 2
If you were doing this by hand, you might just pick the blocks to be moved with one
in each hand and cross your arms to swap them. Or you might move the first one out
of its position temporarily, move the second one in its place, them move the first
one to the now empty position (this is the difference between having a single
function to do the swap, or writing some code to do it).
Step 4. Compare the next block in line with the first, and repeat step 3. Do this until
you run out of blocks. Then begin step one again with the second block. (Fig. 3,4,5,6)
Fig. 3 - Pass #2
Fig. 4 - Pass #3
Fig. 5 - Pass #4
#include<stdio.h>
#include<conio.h>
main()
{
int a[100],n,i;
clrscr();
printf("\n Enter number of elements:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Elements in the list before sorting:\n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
bubblesort(a,n);
printf("\n Elements in the list after sorting:\n");
for(i=0;i<n;i++)
printf(" %5d ",a[i]);
getch();
OUTPUT:
Enter number of elements:5
Enter the elements:89 4 12 41 3
Elements in the list before sorting:
89 4 12 41 3
Elements in the list after sorting:
3 4 12 41 89
Exercise (8b-b): write a c program to implement selection sort
Program:
#include<stdio.h>
main()
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
{
int s,i,j,temp,a[20];
clrscr();
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is: 0 4 5 7 21
Exercise-8(c): write a c program to implement matrixes operations
#include<stdio.h>
main()
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k,ch;
clrscr();
printf("\n menu \n");
printf("\n 1 . Matrix Addition ");
printf("\n 2. Matrix Multiplication ");
printf("\n enter choice ");
scanf("%d",&ch);
printf("\n enter order of 1st matrix ");
scanf("%d%d",&m,&n);
printf("\n enter order of 2nd matrix ");
scanf("%d%d",&p,&q);
if((m==p && n==q) || (n==p))
{
printf("\n enter elements of 1st matrix " );
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n enter elements of 2nd matrix ");
for(i=0;i<p;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
}
else
printf("\n wrong order ");
switch(ch)
{
case 1: printf("\n addition \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
getch();
}
Output :
menu
1. Matrix addition
2. Matrix multiplication
Enter choice 1
Enter order of 1st matrix 2 2
Program:
#include<stdio.h>
struct movie
{
char name[20];
char hero[20];
char heroine[20];
char director[20];
char musicdir[20];
char producer[20];
char price[20];
};
main()
{
Struct movie m1={"janath",”jrNTR”,”Samantha”,”koratala ”,”devisri”,”mythri”,”60cr”};
clrscr();
printf("\n movie name %s",m1.name);
printf("\n movie hero %s",m1.hero);
printf("\n movie heroine %s",m1.heroine);
printf("\n movie director%s",m1.director);
printf("\n music dirctor %s",m1.musicdir);
printf("\n movie producer %s",m1.producer);
printf("\n movie price %s",m1.price);
Output:
movie name janatha
movie hero jrNTR
movie heroine smantha
movie director koratala
music dirctor devisri
movie producer mythri
movie price 60cr
printf(“%s\t%d\t\n”,(ptr+i)->c,(ptr+i)->a);
return 0;
}
Output:
Enter n:2
Enter string and integer respectively:
Programming
22
Enter string and integer respectively:
Structure
33
Displaying information
Programming 22
Structure 33
SAMPLE PROGRAM:
#include<stdio.h>
#include<math.h>
typedef struct
{
float real;
float img;
}COMPLEX;
main()
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
{
COMPLEX c1,c2,cadd;
void real(COMPLEX *);
void write(COMPLEX);
COMPLEX add(COMPLEX, COMPLEX);
clrscr();
printf(“enter first complex number”);
rea(&c1);
printf(“enter second complex number”);
real(&c2);
cadd=add(c1,c2);
printf(“\n first complex number \t”);
write(c1);
printf(“\n Second complex number \t”);
write(c2);
printf(“\n addition of two complex numbers is \t”);
write(cadd);
getch();
}
void real(COMPLEX *cp)
{
printf(“\n enter the real part\t”);
scanf(“%f”,&cp->real);
printf(“enter the imaginary part\t”);
scanf(“%f”,&cp->img);
return;
}
void write(COMPLEX c)
{
printf(“%.2f%+.2fi”,c.real,c.img);
return;
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
}
COMPLEX add(COMPLEX c1,COMPLEX c2)
{
COMPLEX ca;
ca.real=c1.real+c2.real;
ca.img=c1.img+c2.img;
return(ca);
}
SAMPLE OUTPUT:
enter first complex number
enter real part 5
enter imaginary part 2
enter second complex number
enter real part 1
enter imaginary part 2
first complex number 5.00+2.00i
second complex number 1.00+2.00i
addition of two complex numbers is 6.000000+4.000000i
#include<stdio.h>
main()
{
int data[5],i;
clrscr();
printf(“enter elements”);
for(i=0;i<5;i++)
scanf(“%d\t”,data+i);
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
printf(“the elements are”);
for(i=0;i<5;i++)
printf(“%d\t”,*(data+i));
getch();
}
Output:
Enter elements 1 2 3 4 5
The elements are 1 2 3 4 5
Exercise-10(b): Write a c program to find the sum of numbers with arrays and pointers.
Aim: To find the sum of numbers with arrays and pointers.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]);
ptr = numArray; /* a=&a[0] */
for (i = 0; i < 10; i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
}
Output:
Enter 10 elements: 11 12 13 14 15 16 17 18 19 20
The sum of array elements is 155
Exercise-11: Write a c program to implement dynamic memory allocation methods.
Aim: To implement dynamic memory allocation methods.
Program:
Malloc:
#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
Program:
#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL){
printf("Error! memory not allocated.");
exit(0);
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
}
printf("Enter elements of array: ");
for(i=0;i<n;++i){
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
Output:
i)copy:
#include <stdio.h>
#include <string.h>
main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
clrscr();
ii) Concatenate:
#include <stdio.h>
#include <string.h>
main()
{
char s1[10] = "Hello";
char s2[10] = "World";
clrscr();
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
getch();
}
Output:
iii) Length:
#include <stdio.h>
#include <string.h>
main()
{
char str1[20] = "BeginnersBook";
printf("Length of string str1: %d", strlen(str1));
getch();
}
Output:
Iv) Compare:
#include <stdio.h>
#include <string.h>
main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
BONAM VENKATA CHALAMAYYA INSTITUTE OF TECHNOLOGY AND SCIENCE
C PROGRAMMING LAB MANUAL
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
getch();
}
Output:
main()
{
char str[30],i;
printf("\n enter a string ");
gets(str);
i=0;
while(str[i]!='\0')
i++;
printf("\n length of the string is %d",i);
}
Output:
enter a string welcome
Length of the string is 7
ii)Compare:
/* compare two strings */
#include<stdio.h>
main()
{
char str1[20],str2[20];
int i=0,flag;
clrscr();
printf("\n enter a string ");
scanf("%s",str1);
printf("\n enter another string ");
scanf("%s",str2);
while(str1[i]!='0')
{
Output:
enter a string ram
enter another string sai
string1 comes before string2
iii) Concatenate:
/* To concatenate two strings. */
#include<stdio.h>
main()
{
char str1[20],str2[20],str3[20];
int i=0,j=0;
clrscr();
printf("\n enter a string ");
scanf("%s",str1);
printf("\n enter another string ");
scanf("%s",str2);
while(str1[i]!='\0')
{
str3[j]=str1[i];
i++;
j++;
}
i=0;
iv) Copy:
#include<stdio.h>
main()
{
char s1[100], s2[100];
int i;
printf("\nEnter the string :");
gets(s1);
i = 0;
while (s1[i] != '\0') {
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
printf("\nCopied String is %s ", s2);
return (0);
}
Output:
Exercise-13(a): write a c programming code to open a file and to print it contents on screen.
Aim: To open a file and to print it contents on screen.
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file[25];
Output:
Enter the name of file you wish to see
Hello.c
The contents of hello.c file are good morning
Program:
/* copies the contents of one file to another file */
#include<stdio.h>
#include<conio.h>
main()
{
char c,source[20];
FILE *fs,*fp;
clrscr();
printf("\n enter file name ");
gets(source);
fs=fopen(source,"r");
if(fs==NULL)
{
printf("source file cannot open ");
exit(0);
}
fp=fopen("dest.txt","w");
if(fp==NULL)
{
printf("dest file cannot open");
Exercise-14(a): write a c program merges two files and stores their contents in another file
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE *fs1, *fs2, *ft;
printf("Enter name of file which will store contents of two files\n ");
gets(file3);
fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");
ft = fopen(file3,"w");
if( ft == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
fclose(fs1);
fclose(fs2);
fclose(ft);
return 0;
getch();
}
Output:
Enter name of first file x.c
Enter name of second file y.c
Enter name of file which will store contents of two files z.c
Two files were merged into z.c file successfully
Output:
Enter the name of file you wish to delete
bvc.c
bvc.c file deleted successfully