24. Implement call by reference mechanism by swapping two
integers using pointers.
CODE:
#include<stdio.h>
void swap(int *x,int *y){
int t;
t=*x;
*x=*y;
*y=t;
printf("\nValues After Swap: a = %d, b= %d",*x,*y);
}
void main()
{
int a,b;
printf("\nEnter two nos.: ");
scanf("%d%d",&a,&b);
printf("\nValues Before Swap: a = %d, b= %d",a,b);
swap(&a,&b);
}
OUTPUT:
Enter two nos.: 4 11
Values Before Swap: a = 4, b= 11
Values After Swap: a = 11, b= 4
25. Find the number of characters, words and sentences in the
given string.
CODE:
#include<stdio.h>
#include<string.h>
void main(){
char s[200];
int ch=0,sen=0,i;
printf("\nEnter a sentence: ");
gets(s);
for(i=0;s[i]!='\0';i++){
if (s[i]=='.' || s[i]=='?' || s[i]=='!')
sen++;
if(s[i]==' ')
ch++;
}
printf("\nNo. of Characters: %d",ch+1);
printf("\nNo. of words: %d",i);
printf("\nNo. of Sentences: %d",sen);
}
OUTPUT:
Enter a sentence: A for ant. B for bat.
No. of Characters: 6
No. of words: 21
No. of Sentences: 2
26. Copy the contents of one string into another string using
pointers.
CODE:
#include<stdio.h>
#include<string.h>
void main()
{
char given[20],copy[20],*p;
int i;
printf("\nEnter a Sentence: ");
gets(given);
p=&given;
for(i=0;given[i]!='\0';i++)
{
copy[i]=*(p+i);
}
printf("\nCopied String: %s",copy);
printf("\nCopied Successfully!");
}
OUTPUT:
Enter a Sentence: Welcome to CBIT, MCA Department
Copied String: Welcome to CBIT, MCA Department
Copied Successfully!
27. Concatenate two strings without using strcat built-in function.
CODE:
#include<stdio.h>
#include<string.h>
void main()
{
char s1[20],s2[20],s3[40];
int i,s1len,s2len,s3len;
printf("\nEnter first String: ");
gets(s1);
printf("\nEnter second String: ");
gets(s2);
s1len = strlen(s1);
s2len = strlen(s2);
for(i=0;i<s1len;i++){
s3[i] = s1[i];
}
s3[s1len]=' ';
s3len = strlen(s3);
for(i=0;i<s2len;i++){
s3[s3len+i] = s2[i];
}
printf("\nConcatenated String is: %s",s3);
}
OUTPUT:
Enter first String: Master of
Enter second String: Computer Applications
Concatenated String is: Master of Computer Applications
28. Develop functions to perform the following operations on
structure complex.
i) Read a complex number.
ii) Display a complex number.
iii) Add two complex numbers.
CODE:
#include<stdio.h>
struct complex
{
float real;
float img;
};
struct complex read(struct complex x)
{
printf("\nEnter real and imaginary values of complex num: ");
scanf("%f%f",&x.real,&x.img);
return x;
}
struct complex display(struct complex x)
{
printf("%.2f + %.2fi",x.real,x.img);
}
struct complex add(struct complex c3,struct complex c1,struct
complex c2)
{
c3.real=(c1.real + c2.real);
c3.img=(c1.img + c2.img);
return c3;
}
void main()
{
struct complex c1,c2,c3;
c1=read(c1);
c2=read(c2);
c3=add(c3,c1,c2);
printf("\n complex num 1: ");
display(c1);
printf("\n complex num 2: ");
display(c2);
printf("\n Sum of c1 c2 is: ");
display(c3);
}
OUTPUT:
Enter real and imaginary values of complex num: 2 3
Enter real and imaginary values of complex num: 4 5
complex num 1: 2.00 + 3.00i
complex num 2: 4.00 + 5.00i
Sum of c1 c2 is: 6.00 + 8.00i
29. Develop functions to perform the following operations on
structure complex.
i) Read a complex number.
ii) Display a complex number.
iii) Multiply two complex numbers.
CODE:
#include<stdio.h>
struct complex
{
float real;
float img;
};
struct complex read(struct complex x)
{
printf("\nEnter real and imaginary values of complex num : ");
scanf("%f%f",&x.real,&x.img);
return x;
}
struct complex display(struct complex x)
{
printf("%.2f + %.2fi",x.real,x.img);
}
struct complex mul(struct complex c3,struct complex c1,struct
complex c2)
{
c3.real=(c1.real * c2.real)-(c1.img * c2.img);
c3.img=(c1.real * c2.img)+(c1.img * c2.real);
return c3;
}
void main()
{
struct complex c1,c2,c3;
c1=read(c1);
c2=read(c2);
c3=mul(c3,c1,c2);
printf("\n complex num 1 : ");
display(c1);
printf("\n complex num 2 : ");
display(c2);
printf("\n Product of c1 c2 is :");
display(c3);
}
OUTPUT:
Enter real and imaginary values of complex num : 3 -1
Enter real and imaginary values of complex num : 4 5
complex num 1 : 3.00 + -1.00i
complex num 2 : 4.00 + 5.00i
Product of c1 c2 is :17.00 + 11.00i
30. Allocate memory at runtime to store five student records and
also display those students’ records.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int age;
float gpa;
};
int main()
{
struct Student *s;
int n;
printf("\n enter no of records : ");
scanf("%d",&n);
s= (struct Student*) malloc(n * sizeof(struct Student));
for (int i = 0; i < n; i++) {
printf("\n Student - %d",i+1);
printf("\nEnter the name,age,GPA : ");
scanf("%s%d%f", &(s+i)->name,&(s+i)->age,&(s+i)->gpa);
}
printf("\nStudent Records:\n");
for (int i = 0; i < n; i++) {
printf("Name: %s\n", (s+i)->name);
printf("Age: %d\n", (s+i)->age);
printf("GPA: %.2f\n", (s+i)->gpa);
printf("\n");
}
free(s);
return 0;
}
OUTPUT:
enter no of records : 2
Student - 1
Enter the name,age,GPA : maneesh 22 9.7
Student - 2
Enter the name,age,GPA : vamshi 22 10.0
Student Records:
Name: maneesh
Age: 22
GPA: 9.70
Name: vamshi
Age: 22
GPA: 10.00
31. Find out the number of characters, words and sentences in
the given text file.
CODE:
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
int let=0,wrd=0,sen=0;
fp=fopen("sample.txt","r");
while((ch=fgetc(fp))!=EOF)
{
if(ch=='.'||ch=='?'||ch=='?')
sen++;
if(ch==' '||ch=='\n')
wrd++;
let++;
}
printf("\nCount of Characters: %d, Words: %d, Sentences:
%d",let,wrd+1,sen);
}
OUTPUT:
Count of Characters: 21, Words: 4, Sentences: 1
32. Copy the content from one text file to another text file
CODE:
#include <stdio.h>
int main() {
FILE *source, *destination;
char ch;
source = fopen("source.txt", "r");
if (source == NULL) {
printf("Unable to open source file.\n");
return 1;
}
destination = fopen("destination.txt", "w");
if (destination == NULL) {
printf("Unable to create destination file.\n");
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
fclose(source);
fclose(destination);
printf("File copied successfully.\n");
return 0;
}
OUTPUT:
File copied successfully.
33. Read records sequentially from the file.
CODE:
#include<stdio.h>
struct player
{
int ID;
char name[20];
float score;
};
int main(){
FILE *fp;
struct player players[10];
int n,i;
fp=fopen("players.txt","w");
if (fp==NULL)
{
return 1;
}
printf("\nEnter no. of records to write: ");
scanf("%d",&n);
for ( i = 0; i < n; i++)
{
printf("\nEnter ID, name, score: ");
scanf("%d%s%f",&players[i].ID,&players[i].name,&players[i].score);
fwrite(&players[i],sizeof(struct player),1,fp);
}
fclose(fp);
fp = fopen("players.txt","r");
printf("\nReading records sequentially");
for ( i = 0; i < n; i++)
{
fread(&players[i],sizeof(struct player),1,fp);
printf("\n ID : %d, name : %s, score :
%.2f",players[i].ID,players[i].name,players[i].score);
}
return 0;
}
OUTPUT:
Enter no. of records to write: 3
Enter ID, name, score: 1101 Vani 23.4
Enter ID, name, score: 1104 Rani 29.3
Enter ID, name, score: 1109 Sam 31.22
Reading records sequentially
ID : 1101, name : Vani, score : 23.40
ID : 1104, name : Rani, score : 29.30
ID : 1109, name : Sam, score : 31.22
34. Read records randomly from the file based on user choice.
CODE:
#include<stdio.h>
struct player
{
int ID;
char name[20];
float score;
};
int main()
{
FILE *fp;
struct player players[10],p;
int n,i,rec;
fp=fopen("players.txt","w");
if (fp==NULL)
{
return 1;
}
printf("\n enter no. of records to write : ");
scanf("%d",&n);
for ( i = 0; i < n; i++)
{
printf("\n enter ID, name, score : ");
scanf("%d%s%f",&players[i].ID,&players[i].name,&players[i].score);
fwrite(&players[i],sizeof(struct player),1,fp);
}
fclose(fp);
fp =fopen("players.txt","r");
if (fp==NULL)
{
return 1;
}
printf("\n enter record num :");
scanf("%d",&rec);
fseek(fp,(rec-1)*sizeof(struct player),SEEK_SET);
fread(&p,sizeof(struct player),1,fp);
printf("\n ID : %d, name : %s, score : %f",p.ID,p.name,p.score);
fclose(fp);
return 0;
}
OUTPUT:
enter no. of records to write : 2
enter ID, name, score : 1 raj 30
enter ID, name, score : 2 virat 50
enter record num :2
ID : 2, name : virat, score : 50.000000