LAB-8
FUNCTIONS
1. Write a program to implement the string operations like Length of String, String Copying,
String Concatenation, Conversion to Uppercase and String Comparison.( Define own
Function for each of the operation. Header file “string.h” is not allowed)
Source Code:-
#include<stdio.h>
void len(char str[]);
void copy(char s1[],char s2[]);
void concat(char s1[],char s2[]);
void upper(char str[]);
void comp(char s1[],char s2[]);
int main(){
char str[20],s1[20],s2[20],s3[20],s4[20],str1[20],s5[20],s6[20];
len(str);
copy(s1,s2);
concat(s3,s4);
upper(str1);
comp(s5,s6);
return 0;
}
void len(char str[]){
printf("---------LENGTH---------\n");
printf("Enter the string that you want to find the length : ");
scanf("%[^\n]",str);
int l=0,i=0;
while(str[i]!='\0'){
l=l+1;
i++;
}
printf("The length of the string is %d\n",l);
}
void copy(char s1[],char s2[]){
printf("--------COPYING---------\n");
printf("Enter the copying string : ");
gets(s1);
printf("Before copying : ");
printf("s1=%s and s2=%s\n",s1,s2);
printf("After copying : ");
int l=0,i=0;
while(s1[i]!='\0'){
l=l+1;
i++;
}
for(i=0;i<=l;++i){
s2[i]=s1[i];
}
printf("s1=%s and s2=%s \n",s1,s2);
}
void concat(char s1[],char s2[]){
printf("---------CONCATENATION-----------\n");
printf("Enter the source string : ");
gets(s1);
printf("Enter the target string : ");
gets(s2);
int i=0,j=0;
while(s1[i]!='\0'){
i++;
}
while(s2[j]!='\0'){
s1[i]=s2[j];
i++;
j++;
}
s1[i]='\0';
printf("String after concatenation is %s\n",s1);
}
void upper(char str[]){
printf("--------CONVERSION----------\n");
printf("Enter the string : ");
scanf("%[^\n]",str);
int i=0,j,l=0,k,m;
while(str[i]!='\0'){
l=l+1;
i++;
}
for(j=0;j<=l;j++){
k=(int)str[j];
if(k>=97 && k<=122){
m=k-32;
}
else{
m=k;
}
str[j]=(char)m;
}
printf("String after conversion : %s\n",str);
}
void comp(char s1[],char s2[]){
printf("------COMPARISION--------\n");
printf("Enter string 1 : ");
gets(s1);
printf("Enter string 2 : ");
gets(s2);
int len1=0,len2=0,i=0,j=0,k=0,same=0;
while(s1[i]!='\0'){
len1=len1+1;
i++;
}
while(s2[j]!='\0'){
len2=len2+1;
j++;
}
if(len1 == len2)
{
while(k<len1)
{
if(s1[k] == s2[k])
k++;
else break;
}
if(k==len1)
{
same=1;
printf("The two strings are equal\n");
}
}
if(len1!=len2)
printf("The two strings are not equal\n");
if(same == 0)
{
if(s1[k]>s2[k])
printf("String 1 is greater than string 2\n");
else if(s1[k]<s2[k])
printf("String 2 is greater than string 1\n");
}
}
2. Write a C program to implement Multiplication and Division Operations without using
operators
“*” and “\” respectively. Define function “mul” for multiplication and “div” for integer
division.
Source Code:-
#include<stdio.h>
int mul(int a,int b);
int div(int x,int y);
int main(){
int a,b,c,d;
printf("Enter positive numbers for multiplication : ");
scanf("%d%d",&a,&b);
printf("Enter Divident and Divisor:");
scanf("%d%d",&c,&d);
mul(a,b);
div(c,d);
return 0;
}
int mul(int a,int b){
int c=0;
for(int i=1;i<=b;i++){
c=c+a;
}
printf("%d * %d = %d\n",a,b,c);
}
int div(int x,int y){
int q=0;
while(x>=y) {
x=x-y;
q=q+1;
}
printf("Remainder : %d and Quotient : %d\n",x,q);
}