www.computerscienceai.com provides resources like python programs, c programs, java programs, c++ programs, php programs, html and css free resources, articles and "how to" tutorials on computer, science, artificial intelligence and tech world.
strcat(),strcmp(),strrev() User Defined Functions in C
Problem:
Write a C program to implement user defined strcmp(), strcpy(), strrev(), strcat(), strupr(), strlwr() functions in C. Also write main() function to illustrate the use of all user defined functions.
C Program / Source Code:
Here is the source code for strcat() user defined function in C.
/* Aim: For the following standard functions write corresponding user defined functions and write a menu driven program to use them.
1]strcat() 2]strcmp() 3]strrev() 4]strupr() 5]strlwr() 6]strcpy()
*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
void StrCat(char str[],char str1[]); // StrCat() user defined Function in c Prototype
void StrCmp(char str[],char str1[]); // StrCmp() Function Prototype
void StrRev(char str[]); // StrRev() Function Prototype
void StrUpr(char str[]); // StrUpr() Function Prototype
void StrLwr(char str[]); // StrLwr() Function Prototype
void StrCpy(char str[],char str1[]); // StrCpy() Function Prototype
int main()
{
char str[20],str1[20],ch;
int ope;
do{
printf("\n****Menu****");
printf("\n 1.strcat");
printf("\n 2.strcmp");
printf("\n 3.strrev");
printf("\n 4.strupr");
printf("\n 5.strlwr");
printf("\n 6.strcpy");
printf("\n 7.Exit \n");
printf("\n Which function do you want to use:- ");
scanf("%d",&ope);
if(ope==1 || ope==2)
{
printf("\n Enter first string (20 Characters Max):- ");
scanf("%s",str);
printf("\n Enter second string (20 Characters Max):- ");
scanf("%s",str1);
}
else if(ope==3 || ope==4 || ope==5 || ope==6)
{
printf("\n Enter any string (20 Characters Max):- ");
scanf("%s",str);
}
switch(ope)
{
case 1:
StrCat(str,str1);
break;
case 2:
StrCmp(str,str1);
break;
case 3:
StrRev(str);
break;
case 4:
StrUpr(str);
break;
case 5:
StrLwr(str);
break;
case 6:
StrCpy(str1,str); // contents of string are copied into string1
break;
default:
printf("\n Invalid Choice. \n \n");
}}while(ch!=7);
return 0;
} // End of main// StrCat() user defined function in C to concatenate two strings without using string functions
void StrCat(char str[],char str1[])
{
int i,j=0;
for(i=(strlen(str)-1);i<=(strlen(str)+strlen(str1)-2);i++)
{
str[i]=str1[j];
j++;
}
printf("\n The Concatenation of two strings is \"%s\" \n \n",str);
}
// StrCmp() function to compare two strings without string library
void StrCmp(char str[],char str1[])
{
int i,j=0,LED=0;
for(i=0;i<=strlen(str)-1;i++)
{
if(str[i]!=str1[j])
{
LED=1;
break;
}
j++;
}
if(LED==0)
printf("\n Two strings is \"%s\" and \"%s\" are same \n \n",str,str1);
else
printf("\n Two strings is \"%s\" and \"%s\" are not same \n \n",str,str1);
}
// Implicit declaration of StrRev() function to reverse a string without using string functions
void StrRev(char str[])
{
char str1[20];
int i,j=0;
for(i=strlen(str)-1;i>=0;i--)
{
str1[j]=str[i];
j++;
}
printf("\n The reverse string of \"%s\" is \"%s\" \n \n",str,str1);
}
// StrUpr() Function to make all characters in upper case in given string
void StrUpr(char str[])
{
char ch,str1[20];
int i;
for(i=0;i<=strlen(str)-1;i++)
{
ch=toupper(str[i]);
str1[i]=ch;
}
printf("\n The uppercase string of \"%s\" is \"%s\" \n \n",str,str1);
}
// StrLwr() Function to make characters lower case in given string
void StrLwr(char str[])
{
char ch,str1[20];
int i;
for(i=0;i<=strlen(str)-1;i++)
{
ch=tolower(str[i]);
str1[i]=ch;
}
printf("\n The lowercase string of \"%s\" is \"%s\" \n \n",str,str1);
}
// StrCpy function to make to copy a string without using standard library function
void StrCpy(char str[],char str1[])
{
int i,j=0;
for(i=0;str1[i]!='\0';i++)
{
*(str+j)=str1[i];
j++;
}
*(str+j)='\0';
printf("\n Str1 is copied into Str \n");
printf("\n Str= %s \n",str);
}
/* Output of above code / Runtime case:-
****Menu****
1.strcat
2.strcmp
3.strrev
4.strupr
5.strlwr
6.strcpy
7.Exit
Which function do you want to use:- 3
Enter any string (20 Characters Max):-Helloo
The reverse string of "Helloo" is "oolleH"
*/