0% found this document useful (0 votes)
9 views6 pages

Program 9 To 12

Uploaded by

rohanshetty5512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Program 9 To 12

Uploaded by

rohanshetty5512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program - 9

Write functions to implement string operations such as compare, concatenate,


and find string length. Use the parameter passing techniques.
#include<stdio.h>
#include<string.h>
void compare(char [ ],char [ ]);
void concat(char [ ],char [ ]);
void length(char [ ]);

void main( )
{
int n,digit;
char str1[10],str2[10];
do
{
printf("press 1-compare 2-concatenate 3-length of string");
printf("\n enter your choice=");
scanf("%d",&n);
switch(n)
{
case 1:printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
compare(str1,str2);
break;

case 2: printf("enter first string=");


scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
concat(str1,str2);
break;

case 3:printf("enter string=");


scanf("%s",str1);
length(str1);
break;

default: printf("wrong choice");


break;
}
printf("\n Do you want to continue(1/0)? ");
scanf("%d", &digit);
}while(digit==1);
}

void compare(char str1[ ],char str2[ ])


{
int i;
i=strcmp(str1,str2);
if(i==0)
printf("strings are equal\n ");
else
printf("string are not equal\n");
}

void concat(char str1[ ],char str2[ ])


{
strcat(str1,str2);
printf("concatenated string=%s",str1);
}

void length(char str1[ ])


{
int len;
len=strlen(str1);
printf("the length of string=%d",len);
}

Program – 10
Implement structures to read, write and compute average- marks of the
students, list the students scoring above and below the average marks for a
class of N students.
#include<stdio.h>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the details of Student-%d \n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("\n Enter the three subject score=");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
}
Program – 11
Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<math.h>
int main()
{
float a[10],sum=0,mean,temp=0,std,variance,*p;
int i,n;
printf("how many elements:\n");
scanf("%d",&n);
printf("enter array elements:\n");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
p=a;
for(i=0;i<n;i++)
{
sum = sum+*p;
p++;
}
mean= sum/n;

p=a;
for(i=0;i<n;i++)
{
temp=temp+pow((*p-mean),2);
p++;
}
variance=temp/n;
std= sqrt(variance);
printf("sum = %f\n mean= %f\n standard deviation= %f\n",sum,mean,std);
}

Program – 12
Write a C program to copy a text file to another, read both the input file name
and target file name.
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fptr1,*fptr2;
char filename[100], c;
printf("Enter filename to open for reading:");
scanf("%s",filename);
fptr1=fopen(filename,"r");
if(fptr1==NULL)
{
printf("Cannot open file %s\n", filename);
exit(0);
}
printf("Enter filename to open for writing:");
scanf("%s",filename);
fptr2=fopen(filename,"w");
if(fptr2==NULL)
{
printf("Cannot open file %s\n", filename);
exit(0);
}
c=fgetc(fptr1);
while(c!=EOF)
{
fputc(c,fptr2);
c=fgetc(fptr1);
}
printf("\nContents copied to %s",filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}

You might also like