In the previous post we discussed about string handling functions in C. In this post we'll be defining our own version for those function. Let's see how.
xstrlen
As the name suggests xstrlen will be function performing same functions as strlen function which returns the length of the string.
#include<stdio.h>
int xstrlen(char *);
int main()
{
char name[]="Abhilekh";
printf("length is %d",xstrlen(name));
return 0;
}
int xstrlen(char *a)
{
int length = 0;
while(*a!='\0')
{
length++;
a++;
}
return length;
}
Look at the function prototype.
int xstrlen(char *);
It is very clear that the function is expecting a character pointer as a parameter and the function will be returning a integer value.
Let us now see how we are calling the function,
xstrlen(name)
We are passing the name_of_array as a parameter. It should be noted that the name of array is equivalent to the base address of the array.
name is equivalent to &name[0]
Let us now look at the function definition.
int xstrlen(char *a)
{
int length = 0;
while(*a!='\0')
{
length++;
a++;
}
return length;
}
Here we are using the fact that a string always terminates with a null character '\0' .So we just looped through the value at the memory address
The output is:
The length of the string is 8
xstrcpy
As the name suggest xstrcpy is a function which acts similar as strcpy function,which copies one string to another.
#include<stdio.h>
void xstrcpy(char* ,char* );
int main()
{
char name[]="Abhilekh";
char copyName[8];
xstrcpy(copyName,name);
printf("copyName is %s",copyName);
return 0;
}
void xstrcpy(char *a, char *b)
{
int length=0;
while(*b!='\0')
{
*a = *b;
a++;
b++;
}
*a = '\0';
}
Let us look at the function prototype first.
void xstrcpy(char *,char*);
The function is expecting two char
pointers.
The function call is:
xstrcpy(copyName,name);
The function definition is:
void xstrcpy(char *a, char *b)
{
int length=0;
while(*b!='\0')
{
*a = *b;
a++;
b++;
}
*a = '\0';
}
We run the loop until the value at the
string is equal to the null character
and assigned the value at one string
to another until the loop condition
fails.
The output is:
copyName is Abhilekh
xstrcmp
As the name suggest xstrcmp is a function which works similar as strcmp function,It compares two strings and returns the ASCII difference between those strings.
#include<stdio.h>
int xstrcmp(char *,char *);
int main()
{
if(xstrcmp("Kathmandu","Kathman")==0)
printf("Both the strings are same");
else
{
printf("They are different");
}
return 0;
}
int xstrcmp(char *a ,char *b)
{
int diff;
int counter;
int lengtha = 0;
int lengthb = 0;
while(*a !='\0')
{
lengtha++;
a++;
}
while(*b !='\0')
{
lengthb++;
b++;
}
counter=lengtha>lengthb?lengtha:lengthb;
for(int i = 0 ; i< counter ; i++)
{
if(a[i]==b[i])
{
diff = 0;
continue;
}
else
{
diff = a[i] - b[i];
return diff;
}
}
return diff;
}
Similar to above function the function expects two char pointer as its Parameter and returns the difference between the character of the array.
xstrcat
As the name suggest xstrcat is a function which works similar as strcat function, It joins(concatenates) two strings.
#include<stdio.h>
void xstrcat(char *,char*);
int main()
{
char str1[10]="Abhilekh";
char str2[20]= "Gautam";
xstrcat(str2,str1);
printf("Concatinated form is %s",str2);
}
void xstrcat(char *a,char*b)
{
while(*a!='\0')
{
a++;
}
while(*b != '\0')
{
*a = *b;
b++;
a++;
}
*a = '\0';
}
The output is:
Concatinated form is GautamAbhilekh
xstrlwr
As the name suggest xstrlwr is a function which works similar as strlwr function, It converts a string into lowercase.
#include<stdio.h>
void xstrlwr(char *);
int main()
{
char name[]="ABHILEKH";
xstrlwr(name);
printf("Name:%s",name);
return 0;
}
void xstrlwr(char *a)
{
int i=0;
while(*a!='\0')
{
if(*a>=65 && *a<=90)
{
*a = *a + 32;
}
i++;
a++;
}
We use the fact that when a uppercase alphabetical character is added with 32 gives the corresponding lowercase alphabetical character.
The output is:
Name:abhilekh
xstrupr
As the name suggest xstrupr is a function which works similar as strupr function, It converts a string into uppercase.
#include<stdio.h>
void xstrupr(char *);
int main()
{
char name[]="Abhilekh";
xstrupr(name);
printf("Name:%s",name);
return 0;
}
void xstrupr(char *a)
{
int i=0;
while(*a!='\0')
{
if(*a>=97 && *a<=122)
{
*a = *a - 32;
}
i++;
a++;
}
}
We use the fact that when a lowercase alphabetical character is subtracted with 32 gives the corresponding uppercase alphabetical character.
The output is:
Name:ABHILEKH
xstrrev
As the name suggest xstrrev is a function which works similar as strrev function, It reverses a string.
#include<stdio.h>
char * xstrrev(char *);
int main()
{
char name[]="Abhilekh";
printf("Reverse is %s",xstrrev(name));
return 0;
}
char * xstrrev(char *a)
{
int i,j;
char name[8];
for(i = 0 ; a[i]!='\0';i++)
{
name[i]= a[i];
}
name[i] = '\0';
for(i = 0,j=7;name[i]!='\0';i++,j--)
{
a[i] = name[j];
}
a[i] = '\0';
return a;
}
Here the function prototype is
char* xstrrev(char *);
The function returns a character
pointer.
The output is:
The reversed form is hkelihbA
All the above codes are available in this git repo as well.
That's It!
Happy Coding
Comments
Post a Comment