DATA STRUCTURE LAB ASSIGNMENT
Samaseen Prabhat
22MM8037
Page |1
DATA STRUCTURE LAB
ASSIGNMENT(3)
//CALL BY REFERENCE
#include<stdio.h>
void swap(int *,int *);
int main()
{
int n1,n2;
printf("'\nFunction : swap two numbers using
function :\n");
printf("------------------------------------------------\n");
printf("Via call by reference\n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);
printf("Before swapping:n1=%d,n2=%d
",n1,n2);
swap(&n1,&n2);
printf("\nAfter swapping:n1=%d,n2=%d\n\
n",n1,n2);
Page |2
return 0;
}
void swap(int*p,int*q)
{
int tmp;
tmp=*p;
*p=*q;
*q=tmp;
}
Page |3
//CALL BY VALUE
#include<stdio.h>
void swap(int,int);
int main()
{
int n1,n2;
printf("'\nFunction : swap two numbers using
function :\n");
printf("------------------------------------------------\n");
printf("Via call by value\n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);
printf("Before swapping:n1=%d,n2=
%d",n1,n2);
swap(n1,n2);
printf("\nAfter swapping:n1=%d,n2=%d\n\
n",n1,n2);
return 0;
}
void swap(int p,int q)
{
Page |4
int tmp;
tmp=p;
p=q;
q=tmp;
}
Page |5
//USING TWO VARIABLES CALL BY VALUE.
#include<stdio.h>
void swap(int,int);
int main()
{
int n1,n2;
printf("'\nFunction : swap two numbers using
function :\n");
printf("------------------------------------------------\n");
printf("Via call by value using two variables\
n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);
printf("Before swapping:n1=%d,n2=
%d",n1,n2);
swap(n1,n2);
printf("\nAfter swapping:n1=%d,n2=%d\n\
n",n1,n2);
return 0;
}
void swap(int p,int q)
Page |6
{
p=p-q;
q=p+q;
p=q-p;
}
Page |7
//USING TWO VARIABLES CALL BY
REFERENCE.
#include<stdio.h>
void swap(int *,int *);
int main()
{
int n1,n2;
printf("'\nFunction : swap two numbers using
function :\n");
printf("------------------------------------------------\n");
printf("Via call by reference using two
variables\n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);
printf("Before swapping:n1=%d,n2=%d
",n1,n2);
swap(&n1,&n2);
printf("\nAfter swapping:n1=%d,n2=%d\n\
n",n1,n2);
return 0;
}
Page |8
void swap(int*p,int*q)
{
*p=*p-*q;
*q=*p+*q;
*p=*q-*p;
}
Page |9
//I. Write a code to input your full name and
print it
//a. Using scanf and printf
#include<stdio.h>
int main()
{
char Name[100],name[100];
printf("Enter your name:");
scanf("%s",&Name);
printf("Name entered by using scanf and printf
is %s",Name);
return 0;
}
P a g e | 10
//b. Using fgets and puts
#include <stdio.h>
int main()
{
char fullName[30];
printf("Type your full name:");
fgets(fullName, 30, stdin);
printf("Name entered by using fgets and puts
is");printf("\t");puts(fullName);
return 0;
}
P a g e | 11
//II. Write a code to find the length of a
string without using library functions
//and also with using library function.
//1. WITHOUT USING LIBRARY FUNCTION
#include <stdio.h>
int main()
{
char str[100];
int i,length=0;
printf("Type your name:");
fgets(str,100,stdin);
for(i=0; str[i]!='\0'; i++)
{
length++;
}
printf("Length of name without using library
function is %d",length-2);
return 0;
}
P a g e | 12
P a g e | 13
//2. WITH USING LIBRARY FUNCTION.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len;
printf("Type your name:");
fgets(str,100,stdin);
len = strlen(str);
P a g e | 14
printf("Length of string with using library
function is: %d",len-2);
return 0;
}
//III. Write a code to print the string by
calling a function.
#include <stdio.h>
void print_string(char *str)
{
printf("Entered string is %s",str);
}
int main()
{
char *str;
P a g e | 15
printf("Enter a string:");
fgets(str,100,stdin);
print_string(str);
return 0;
}
//IV. Write a code to print individual
characters of a string in reverse order.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
P a g e | 16
char str[100];
int l, i;
printf("Input the string : ");
fgets(str,100,stdin);
l=strlen(str);
printf("The characters of the string in reverse
are : ");
for (i = l - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
return 0;
}
P a g e | 17
P a g e | 18
//V. Write a code to compare two strings
without using string library functions.
#include <stdio.h>
int compare_strings(char *str1, char *str2)
{
int i = 0;
while (str1[i]!='\0'&&str2[i]!='\0')
{
if (str1[i]!=str2[i])
{
return str1[i]-str2[i];
}
i++;
}
if (str1[i]=='\0'&&str2[i]=='\0')
{
return 0;
}
else if (str1[i]=='\0')
{
return -1;
}
else
P a g e | 19
{
return 1;
}
}
int main()
{
char str1[100];
char str2[100];
printf("Input the string_1 : ");
fgets(str1,100,stdin);
printf("Input the string_2 : ");
fgets(str2,100,stdin);
int result=compare_strings(str1, str2);
if (result==0)
{
printf("The strings are equal.");
}
else if(result < 0)
{
printf("The strings are not equal.");
}
else
P a g e | 20
{
printf("The strings are not equal.");
}
return 0;
}
P a g e | 21
// Write a code to compare two strings with
using string library functions.
#include<stdio.h>
#include<string.h>
void main()
{
char str1[100],str2[100];
int value;
printf("Enter String 1: ");
fgets(str1,100,stdin);
printf("Enter String 2: ");
fgets(str2,100,stdin);
value=strncmp(str1,str2,4);
if(value==0)
{
printf("%s is same as %s",str1,str2);
}
else if(value>0)
{
printf("%s is not same as %s",str1,str2);
}
else
{
P a g e | 22
printf("%s is not same as %s",str1,str2);
}
}
P a g e | 23
//VII. Write a code to copy one string to
another string.
#include <stdio.h>
int main()
{
char s1[1000],s2[1000];
int i;
printf("\n\n");
printf("Enter any string: ");
gets(s1);
for(i=0;s1[i]!='\0';i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("original string s1='%s'\n",s1);
printf("copied string s2='%s'\n\n",s2);
return 0;
}
P a g e | 24
P a g e | 25
//Write a code to count the total number of
alphabets,
//digits and special characters in a string.
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int i,alpha=0,dgts=0,spclchar=0;
printf("\n\n");
printf("Enter the string : ");
gets(s);
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 &&
s[i]<=122) )
alpha++;
else if(s[i]>=48 && s[i]<=57)
dgts++;
else
spclchar++;
}
P a g e | 26
printf("Alphabets = %d\n",alpha);
printf("Digits = %d\n",dgts);
printf("Special characters = %d\n", spclchar);
return 0;
}
P a g e | 27
//Write a code to count the total number of
vowels or consonants in a string.
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int i,vowels=0,consonants=0;
printf("Enter the string : ");
gets(s);
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 &&
s[i]<=122))
{
if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||
s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||
s[i]=='I'||s[i]=='O' ||s[i]=='U')
vowels++;
else
consonants++;
}
}
P a g e | 28
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
return 0;
}
P a g e | 29
//Write code to read a string from the
keyboard and sort it using bubble sort.
#include <stdio.h>
#include <string.h>
void bubbleSort(char arr[][100], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (strcmp(arr[j], arr[j + 1]) > 0)
{
char temp[100];
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], temp);
}
}
}
}
int main()
{
int num_strings;
P a g e | 30
printf("Input number of strings: ");
scanf("%d", &num_strings);
char strings[num_strings][100];
for (int i = 0; i < num_strings; i++)
{
printf("Input string %d: ", i + 1);
scanf("%s", strings[i]);
}
bubbleSort(strings, num_strings);
printf("The strings appear after sorting:\n");
for (int i = 0; i < num_strings; i++)
{
printf("%s ", strings[i]);
}
return 0;
}
P a g e | 31
P a g e | 32
//Write a code to split full name into First,
Middle and Last Name.
#include <stdio.h>
#include<string.h>
int main()
{
char name[50] = "\0";
char fname[50] = "\0";
char mname[50]= "\0";
char lname[50]= "\0";
char * ptr = NULL;
int count =0;
int i,j,k,l;
j=k=l=0;
printf("\nEnter you full name (max 50
chararacter) : ");
gets(name);
for(i=0; i<strlen(name); i++)
{
if(name[i] == ' ')
count++;
if(count ==0)
P a g e | 33
fname[j++] = name[i];
if(count ==1)
mname[k++] = name[i];
if(count ==2)
lname[l++] = name[i];
}
printf("\n fname : %s", fname);
if(strlen(lname)==0)
{
printf("\n lname : %s", mname);
}
else
{
printf("\n mname : %s", mname);
printf("\n lname : %s", lname);
}
return 0;
}
P a g e | 34
P a g e | 35
//Write a code to print the full name into a
short name.
#include<stdio.h>
int main()
{
char fname[20], mname[20], lname[20];
printf("\nEnter The First Name, Middle Name
and Last Name: ");
scanf("%s %s %s", fname, mname, lname);
printf("Abbreviated Name: ");
printf("%c.%c. %s\n", fname[0], mname[0],
lname);
return 0;
}