1. Write a program in C to compare two strings without using string library functions.
SOURCE CODE:
#include<stdio.h>
int main(){
char str1[50],str2[50];
int i;
printf("Enter string1:\n");
gets(str1);
printf("Enter string2:\n");
gets(str2);
for(i=0;str1[i]!='\0' || str2[i]!='\0';i++)
if(str1[i]!=str2[i])
break;
if(str1[i]-str2[i]==0)
printf("Both strings are equal.");
else if(str1[i]-str2[i]>0)
printf("String1 is greater than String2.");
else
printf("String1 is lesser than String2.");
return 0;
}
OUTPUT-1:
Enter string1:
there
Enter string2:
there
Both strings are equal.
OUTPUT-2:
Enter string1:
their
Enter string2:
there
String1 is lesser than String2.
OUTPUT-3:
Enter string1:
there
Enter string2:
their
String1 is greater than String2.
2. Write a program in C to copy one string to another string.
SOURCE CODE:
#include<stdio.h>
int main(){
char str1[30],str2[30];
int i;
printf("Enter string:\n");
gets(str1);
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
printf("Copied string is:\n");
puts(str2);
}
OUTPUT:
Enter string:
hello world
Copied string is:
hello world
SOURCE CODE:
#include<stdio.h>
int main(){
char str1[50],str2[50];
int i;
printf("Enter string1:\n");
gets(str1);
printf("Enter string2:\n");
gets(str2);
for(i=0;str1[i]!='\0' || str2[i]!='\0';i++)
if(str1[i]!=str2[i])
break;
if(str1[i]-str2[i]==0)
printf("Both strings are equal.");
else if(str1[i]-str2[i]>0)
printf("String1 is greater than String2.");
else
printf("String1 is lesser than String2.");
return 0;
}
OUTPUT-1:
Enter string1:
there
Enter string2:
there
Both strings are equal.
OUTPUT-2:
Enter string1:
their
Enter string2:
there
String1 is lesser than String2.
OUTPUT-3:
Enter string1:
there
Enter string2:
their
String1 is greater than String2.
2. Write a program in C to copy one string to another string.
SOURCE CODE:
#include<stdio.h>
int main(){
char str1[30],str2[30];
int i;
printf("Enter string:\n");
gets(str1);
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
printf("Copied string is:\n");
puts(str2);
}
OUTPUT:
Enter string:
hello world
Copied string is:
hello world