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[20];
   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[25]="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!
Labels:
C/C++ Language
 Previous Article
                          
Responses
0 Respones to "How String Functions (string.h) Work? in C++ Part -1"
Post a Comment