Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * string.h
- */
- #ifndef __STRING_H__
- #define __STRING_H__
- int strlen(const char *s);
- int strcmp(const char *s1, const char *s2);
- int strcasecmp(const char *s1, const char *s2);
- int strncmp(const char *s1, const char *s2, int l);
- int strncasecmp(const char *s1, const char *s2, int l);
- char *strcpy(char *s1, const char *s2);
- char *strncpy(char *s1, const char *s2, int l);
- char *strcat(char *s1, const char *s2);
- char *strncat(char *s1, const char *s2, int l);
- char toupper(char c);
- char tolower(char c);
- char *strchr(const char *s, char c);
- char *strnchr(const char *s, char c, int l);
- char *strrchr(const char *s, char c);
- char *strrnchr(const char *s, char c, int l);
- char *strstr(const char *s1, const char *s2);
- int isalnum(char c);
- int isaplha(char c);
- int isdigit(char c);
- int islower(char c);
- int isupper(char c);
- int isascii(char c);
- int iscntrl(char c);
- int isgraph(char c);
- int isprint(char c);
- int ispunct(char c);
- int isspace(char c);
- int isxdigit(char c);
- int memcmp(const void *m1, const void *m2, int l);
- void *memcpy(const void *m1, const void *m2, int l);
- void *memset(void *m1, int l, int v);
- void swap(int *a, int *b);
- #endif // __STRING_H__
- /**
- * string.c
- */
- #include "string.h"
- #define NULL (0)
- int strlen(const char *s)
- {
- int i = 0;
- while(*s++)
- {
- i++;
- }
- return i;
- }
- int strcmp(const char *s1, const char *s2)
- {
- while(*s1 && *s2)
- {
- if (*s1 > *s2)
- {
- return 1;
- }
- if (*s1 < *s2)
- {
- return -1;
- }
- s1++;
- s2++;
- }
- if (*s1 && !*s2)
- {
- return 1;
- }
- if (!*s1 && *s2)
- {
- return -1;
- }
- return 0;
- }
- int strcasecmp(const char *s1, const char *s2)
- {
- char c1, c2;
- while(*s1 && *s2)
- {
- c1 = toupper(*s1);
- c2 = toupper(*s2);
- if (c1 > c2)
- {
- return 1;
- }
- if (c1 < c2)
- {
- return -1;
- }
- s1++;
- s2++;
- }
- if (*s1 && !*s2)
- {
- return 1;
- }
- if (!*s1 && *s2)
- {
- return -1;
- }
- return 0;
- }
- int strncmp(const char *s1, const char *s2, int l)
- {
- while(*s1 && *s2 && (l-- > 0))
- {
- if (*s1 > *s2)
- {
- return 1;
- }
- if (*s1 < *s2)
- {
- return -1;
- }
- s1++;
- s2++;
- }
- if (l > 0)
- {
- if (*s1 && !*s2)
- {
- return 1;
- }
- if (!*s1 && *s2)
- {
- return -1;
- }
- }
- return 0;
- }
- int strncasecmp(const char *s1, const char *s2, int l)
- {
- char c1, c2;
- while(*s1 && *s2 && (l-- > 0))
- {
- c1 = toupper(*s1);
- c2 = toupper(*s2);
- if (c1 > c2)
- {
- return 1;
- }
- if (c1 < c2)
- {
- return -1;
- }
- s1++;
- s2++;
- }
- if (l > 0) {
- if (*s1 && !*s2)
- {
- return 1;
- }
- if (!*s1 && *s2)
- {
- return -1;
- }
- }
- return 0;
- }
- char *strcpy(char *s1, const char *s2)
- {
- char *p = s1;
- while (*s2)
- {
- *p++ = *s2++;
- }
- *p = '\0';
- return(s1);
- }
- char *strncpy(char *s1, const char *s2, int l)
- {
- char *p = s1;
- while (*s2 && (l-- > 0))
- {
- *p++ = *s2++;
- }
- if (l > 0)
- {
- *p = '\0';
- }
- return s1;
- }
- char *strcat(char *s1, const char *s2)
- {
- char *p = s1;
- while (*p)
- {
- p++;
- }
- while (*s2)
- {
- *p++ = *s2++;
- }
- *p = '\0';
- return s1;
- }
- char *strncat(char *s1, const char *s2, int l)
- {
- char *p = s1;
- while (*p)
- {
- p++;
- }
- while (*s2 && (l-- > 0))
- {
- *p++ = *s2++;
- }
- *p = '\0';
- return s1;
- }
- char toupper(char c)
- {
- if (c >= 'a' && c <= 'z')
- {
- c -= 32;
- }
- return c;
- }
- char tolower(char c)
- {
- if (c >= 'A' && c <= 'Z')
- {
- c += 32;
- }
- return c;
- }
- char *strchr(const char *s, char c)
- {
- while (*s)
- {
- if (*s == c)
- {
- return (char *)s;
- }
- s++;
- }
- return NULL;
- }
- char *strnchr(const char *s, char c, int l)
- {
- while (*s && (l-- > 0))
- {
- if (*s == c)
- {
- return (char *)s;
- }
- s++;
- }
- return NULL;
- }
- char *strrchr(const char *s, char c)
- {
- char *p = (char *)s;
- while(*p)
- {
- p++;
- }
- while (p != s)
- {
- if (*p == c)
- {
- return (char *)p;
- }
- p--;
- }
- return NULL;
- }
- char *strrnchr(const char *s, char c, int l)
- {
- char *p = (char *)s;
- while(*p)
- {
- p++;
- }
- while (p != s && (l-- > 0))
- {
- if (*p == c)
- {
- return (char *)p;
- }
- p--;
- }
- return NULL;
- }
- char *strstr(const char *s1, const char *s2)
- {
- char *p = (char *)s2;
- char *r = NULL;
- int c = 0, d = 0;
- while (*p++)
- {
- c++;
- }
- p = (char *)s2;
- d = c;
- while (*s1 && (c > 0))
- {
- if (*s1 != *p)
- {
- s1++;
- p = (char *)s2;
- r = NULL;
- c = d;
- }
- else
- {
- if (!r)
- {
- r = (char *)s1;
- }
- s1++;
- p++;
- c--;
- }
- }
- if (c != 0)
- {
- r = NULL;
- }
- return r;
- }
- int isalnum(char c)
- {
- if ((c >= '0' && c <= '9') ||
- (c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z'))
- {
- return 1;
- }
- return 0;
- }
- int isalpha(char c)
- {
- if ((c >= 'A' && c <= 'Z') ||
- (c >= 'a' && c <= 'z'))
- {
- return 1;
- }
- return 0;
- }
- int isdigit(char c)
- {
- if (c >= '0' && c <= '9')
- {
- return 1;
- }
- return 0;
- }
- int islower(char c)
- {
- if (c >= 'a' && c <= 'z')
- {
- return 1;
- }
- return 0;
- }
- int isupper(char c)
- {
- if (c >= 'A' && c <= 'Z')
- {
- return 1;
- }
- return 0;
- }
- int isascii(char c)
- {
- if ((unsigned int)c <= 0x7F)
- {
- return 1;
- }
- return 0;
- }
- int iscntrl(char c)
- {
- if ((c >= 0 && c <= 0x1F) || c == 0x7F)
- {
- return 1;
- }
- return 0;
- }
- int isgraph(char c)
- {
- if ((unsigned int)c > 0x7F)
- {
- return 1;
- }
- return 0;
- }
- int isprint(char c)
- {
- if (c >= 0x20 && c < 0x7F)
- {
- return 1;
- }
- return 0;
- }
- int ispunct(char c)
- {
- if ((c >= ' ' && c <= '/') ||
- (c >= ':' && c <= '@') ||
- (c >= '[' && c <= '`') ||
- (c >= '{' && c <= '~'))
- {
- return 1;
- }
- return 0;
- }
- int isspace(char c)
- {
- if (c == ' ' || c == '\t')
- {
- return 1;
- }
- return 0;
- }
- int isxdigit(char c)
- {
- if ((c >= '0' && c <= '9') ||
- (c >= 'A' && c <= 'F') ||
- (c >= 'a' && c <= 'f'))
- {
- return 1;
- }
- return 0;
- }
- int memcmp(const void *m1, const void *m2, int l)
- {
- const unsigned char *d;
- const unsigned char *s;
- d = (const unsigned char *)m1;
- s = (const unsigned char *)m2;
- while (l-- > 0)
- {
- if (*d > *s)
- {
- return 1;
- }
- if (*d < *s)
- {
- return -1;
- }
- d++;
- s++;
- }
- return 0;
- }
- void *memcpy(const void *m1, const void *m2, int l)
- {
- unsigned char *d;
- const unsigned char *s;
- d = (unsigned char *)m1;
- s = (const unsigned char *)m2;
- while (l-- > 0)
- {
- *d++ = *s++;
- }
- return (void *)m1;
- }
- void *memset(void *m, int v, int l)
- {
- unsigned char *d;
- d = (unsigned char *)m;
- while (l-- > 0)
- {
- *d++ = (unsigned char)v;
- }
- return m;
- }
- void swap(int *a, int *b)
- {
- int t = *a;
- *a = *b;
- *b = t;
- }
- // End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement