Develop your own functions for performing following operations on strings:
(a) Copying one string to another
(b) Comparing two strings
(c) Adding a string to the end of another string
Write a driver program to test your functions.
#include <stdio.h>
void Copy(char str[20]);
void Compare(char str1[20],char str2[20]);
void Add(char str1[20],char str2[20]);
void main()
{
char str[20],str1[20],str2[20];
clrscr();
printf("Enter a String:");
scanf("%s",str);
Copy(str);
printf("\nEnter 1st string to compare:");
scanf("%s",str1);
printf("\nEnter 2nd string to compare:");
scanf("%s",str2);
Compare(str1,str2);
Add(str1,str2);
getch();
}
void Copy(char str[20])
{
char string[20];
strcpy(string,str);
printf("Copied string: %s",string);
}
void Compare(char str1[20],char str2[20])
{
if(strcmp(str1,str2)==0)
printf("\nStrings are Equal!");
else
printf("\nStrings are not Equal!");
}
void Add(char str1[20],char str2[20])
{
strcat(str1,str2);
printf("\nAdding of String\n%s",str1);
}
(a) Copying one string to another
(b) Comparing two strings
(c) Adding a string to the end of another string
Write a driver program to test your functions.
#include <stdio.h>
void Copy(char str[20]);
void Compare(char str1[20],char str2[20]);
void Add(char str1[20],char str2[20]);
void main()
{
char str[20],str1[20],str2[20];
clrscr();
printf("Enter a String:");
scanf("%s",str);
Copy(str);
printf("\nEnter 1st string to compare:");
scanf("%s",str1);
printf("\nEnter 2nd string to compare:");
scanf("%s",str2);
Compare(str1,str2);
Add(str1,str2);
getch();
}
void Copy(char str[20])
{
char string[20];
strcpy(string,str);
printf("Copied string: %s",string);
}
void Compare(char str1[20],char str2[20])
{
if(strcmp(str1,str2)==0)
printf("\nStrings are Equal!");
else
printf("\nStrings are not Equal!");
}
void Add(char str1[20],char str2[20])
{
strcat(str1,str2);
printf("\nAdding of String\n%s",str1);
}
No comments:
Post a Comment