Tuesday, 23 December 2014

Code for string library functions

                   String Copy


char *strcpy( char            *s1, const char   *s2)
{
      char        *s = s1;

      while ((*s1++ = *s2++));
      return s;
}


String Length

int strlen(const char *s)
{
      int   l = 0;

      while (*s++) l++;
      return l;
}
String concat

char *strcat(char *s1, char *s2)
{
      char  *s = s1;

      while (*s1) s1++;
      while (*s2 ) {
            *s1++ = *s2++;
            n--;
      }
      *s1='\0';
      return s;
}
String compare

int strcmp(const char   *s1,const char    *s2)
{
      while ((*s1 || *s2) && n--)
            if (*s1++ != *s2++)
                  return (*--s1 - *--s2);
      return 0;
}
To lower

char tolower(char c)
{
      if (isupper(c))
        return c + ('a' - 'A');
        else
        return c;
}
To upper
char toupper(char c)
{
  if (islower(c))
      return c - ('a' - 'A');
  else
      return c;

}

No comments:

Post a Comment