The above string functions can be written as user defined functions. In this section, some of the user defined functions are given. These functions can be called in the same way as predefined functions.
User defined function for strlen()
We increment the pointer till reaches ‘\0’ and use a counter to count.
int slen(char*s)
{
int count=0;
while (*s!=’\0’)
{
count++;
s++;
}
return count;
}
void main()
{
char str[80];
gets(str);
printf(“The length is %d”,slen(str));
}
User defined function for strcpy()
In this function, we copy each character from the source string to the target string using pointers and return the target string. char * scpy(char * target, char * source)
{
char *t=target;
while(*source !=‘\0’)
{
target=*source;
target++;
*source++;
}
target=’\0’;
return t;
}
User defined function for strncpy(): In this function, we copy the first n characters from this source string to the target string pointers and return the target string. char* sncpy(char * target, char * source, int n)
{
char *t=target;
for(int i=0; *source !=’\0’ && i<n; i++)
{
target=*source;
target++;
source++;
}
target=’\0’;
return t;
}
User defined function for strcat()
In this function, we append the characters from the second string to the end of the first and return the first string. For this we move the pointer of the first to the end and then copy each character from the second string. A NULL character is then added at the end of the string.
char * scat (char * s1,char *s2)
{
char *t=s1;
while(*s1!=’\0’)
s1++;
while(*s2!=’\0’)
*s1++=*s2++;
*s1=’\0’;
return t;
}
User defined function for strncat()
In this function , we append the first n characters from second string to end of the first and return the first string. char * sncat(char * s1,char *s2,int n)
{
char *t=s1; int i;
while(*s1!=’\0’)
s1++;
for(i=0; *s2 !=’\0’ && i<n; i++)
*s1++=*s2++;
**s1=’\0’;
return t;
}
User defined function for strupr()
In this function, we convert all lowercase character to uppercase. To check if a character is lowercase, we use the function islower() (from ctype.h).To convert the character to uppercase, we use the function toupper()
char *supr(char *s)
{
char *t=s;
while(*s!=’\0’)
{
if(islower(*s))
*s=toupper(*s);
*s++;
}
return t;
}
User defined function for strlwr()
In this function, we convert all uppercase character to lowercase. To check if a character is upper case, we use the function isupper() (from ctype.h). To convert the character to lowercase, we use the function tolower()
*char *slwr(char *s)
{
char *t=s;
while(*s!=’\0’)
{
if(isupper(*s))
*s=tolower(*s);
s++;
}
return t;
}
User defined function for strcmp()
In this function we compare two string by comparing each character. If the characters do not match, we return the difference. If both string are identical, a 0 is returned.
int scmp(char * s1, char *s2)
{
while((*s1!=’\0’)&&(*s2!=’\0’)&&(*s1==*s2))
{
s1++;
s2++;
}
if(*s1==*s2)
return 0;
else
return *s1-*s2;
}
User defined function for strcmpi()
This function is identical to the above. To ignore the case, we convert both strings to uppercase and then compare. To convert the string to uppercase, we use the predefined function strupr(from string.h)
int scmpi(char * s1, char *s2)
{
/*convert both strings to uppercase*/
strupr(s1); strupr(s2);
while((*s1!=’\0’)&&(*s2!=’\0’)&&(*s1==*s2))
{
s1++;
s2++;
}
if(*s1==*s2)
return 0;
else
return *s1-*s2;
}
User defined function for strrev()
This function reverses a string and returns the reverse string. The logic is to swap the first and last, second and second-last character and so on the we reach the middle of the string. char * srev(char* str)
{ int l, i, m;
char *bptr, *eptr, ch;
l = strlen(str);
bptr = str; eptr = str;
for (i = 0; i < l-1 ; i++)
eptr++;
m=(int)l/2;
for (i = 0; i < m; i++)
{ ch = *eptr;
*eptr = *bptr;
**bptr = ch;
bptr++;
eptr--;
}
return str;
}
User defined function for strset()
This function sets all character in pointed to by to be value of ch. In this function, a loop starts from the first character and goes up to the last and replaces each character by ch.
char * sset(char *s, char ch)
{
int i;
for(i=0; s[i]!=‘\0’; i++)
s[i] = ch; //or*(s+i) = ch
return s;
}
User defined function for strchr()
This function checks for the occurrence of a character in the string and returns a pointer to the first occurrence of the character. If returns NULL if not found.
char *schr(char *s, char ch)
{
int i;
for(i=0; s[i]!=`\0`;i++)
if (s[i] == ch) //or *(s+i) == ch
return &s[i];
return NULL; /* not found */
}
User defined function for strrchr()
This function checks for the occurrence of a specific character in the string and returns a pointer to the last occurrence of the character. If returns NULL if not found. We can modify the function above to search the string in the reverse order.
char *srchr(char *s, char ch)
{
int i, len;
len = strlen(s);
for(i=len-1; i>=0; i--)
if (s[i] == ch) //or * (s+i) == ch
return &s[i];
return NULL;
}
User defined function for strstr()
This function takes two string as argument s1 and s2 and returns a pointer to the first occurrence of s2 in s1. Returns NULL if no match is found.
char * sstr(char *s1,char *s2)
{
char *p1 = s1, *p1match, *p2;
if(s2==NULL) return s1;
while((*p1 !=‘\0’)
{
p1match=p1,p2=s2;
while((*p1 !=‘\0’) && (*p2!=‘\0’) && (*p1 == *p2))
{
p1++;
p2++;
}
if(*p2==‘\0’)
return p1match ;
p1=p1match+ 1;
}
return NULL;
}
Comments
Post a Comment
Please give us feedback through comments