## Thursday, July 26, 2007

### How String Functions (string.h) Work?

In the previous article String Manipulation Functions (string.h), we had a look at some of the commonly used string manipulation functions. There is no denying the fact that those functions are useful but have you ever wondered how those functions actually work or what is the algorithm behind their working?

If yes then read on…

In this article I am going to present you with our own version of the string manipulation functions that we had discussed, namely strlen(), strcpy(), strcat() and strcmp(). Our versions will do the same thing as done by the original functions but surely they would teach us a lot!

Let's have a look at them one-by-one:

mystrlen

```  // mystrlen- function
#include<iostream.h>

int mystrlen(const char *);

void main(void)
{
char ch[]="This is great!";
cout<<"Length:"<<mystrlen(ch);
}

int mystrlen(const char *str)
{
int len=0;

while(str[len]!='\0')
len++;
return len;
}```

mystrcpy

```  // mystrcpy- function
#include<iostream.h>

void mystrcpy(char *,const char *);

void main(void)
{
char ch[]="This is great!";
char ch2;

mystrcpy(ch2,ch);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrcpy(char *str1,const char *str2)
{
int i=0;

// copy each character
while(str2[i]!='\0')
{
str1[i]=str2[i];
i++;
}
// put the end of
// string identifier
str1[i]='\0';
}```

mystrcat

```  // mystrcat- function
#include<iostream.h>

void mystrcat(char *,const char *);

void main(void)
{
char ch[]="This is great!";
char ch2="Yes ";

mystrcat(ch2,ch);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrcat(char *str1,const char *str2)
{
int i=0;
int len=0;

// skip to the end of the first
// string(target)
while(str1[len]!='\0')
len++;

// start copying characters
// from the start of the
// second string (source)
// to the end of the
// first (target)
while(str2[i]!='\0')
{
str1[len]=str2[i];
i++;len++;
}
str1[len]='\0';
}```

mystrcmp

```  // mystrcmp- function
#include<iostream.h>

int mystrcmp(char *,const char *);

void main(void)
{
char ch[]="C++";
char ch2[]="C++";

cout<<mystrcmp(ch2,ch);
}

int mystrcmp(char *str1,const char *str2)
{
int i=0,cmp=-1;

while(str1[i]!='\0')
{
if(str1[i]==str2[i])
{
// check one character
// following it, so that
// end of string is also
// compared
if(str1[i+1]==str2[i+1])
cmp=0;
// if not same then check
// to see which string has
// higher ASCII value for the
// non-matching charcter
else if(str1[i+1]<str2[i+1])
{
cmp=-1;
break;
}
else
{
cmp=1;
break;
}
}
else if(str1[i]<str2[i])
{
cmp=-1;
break;
}
else
{
cmp=1;
break;
}
i++;
}
return cmp;
}```

Good-Bye!

Related Articles:

#### 5 comments:

1. Anonymous11:59 PM

That was easy to understand
great tutorial

2. thanks!

Arvind

3. This comment has been removed by a blog administrator.

4. Plz don't spam.

5. Thanks for the article it was interesting

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.