Commit 63d7197e by malcolm

Added more string functions

parent 3e410fcd
Showing with 64 additions and 1 deletions
#include "string.h"
#include <stdbool.h>
void *memset(void *s, int c, size_t n){
unsigned char* p=s;
......@@ -48,3 +49,62 @@ size_t strlen(const char* str)
ret++;
return ret;
}
int strcmp(const char* s1, const char* s2)
{
while(*s1 && (*s1==*s2))
{
s1++,s2++;
}
return *(const unsigned char*)s1-*(const unsigned char*)s2;
}
int strncmp(const char *s1, const char *s2, size_t n)
{
for ( ; n > 0; s1++, s2++, --n)
if (*s1 != *s2)
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
else if (*s1 == '\0')
return 0;
return 0;
}
int atoi(char *p)
{
int k = 0;
while (*p) {
k = (k<<3)+(k<<1)+(*p)-'0';
p++;
}
return k;
}
bool isNumericChar(char x)
{
return (x >= '0' && x <= '9')? true: false;
}
int stoi(char *str)
{
if (*str == NULL)
return 0;
int res = 0;
int sign = 1;
int i = 0;
if (str[0] == '-')
{
sign = -1;
i++;
}
for (; str[i] != '\0'; ++i)
{
if (isNumericChar(str[i]) == false)
return 0;
res = res*10 + str[i] - '0';
}
return sign*res;
}
......@@ -5,5 +5,8 @@
void* memset(void *s, int c, size_t n);
void itoa(int n, char s[]);
size_t strlen(const char* str);
int strcmp(const char* s1, const char* s2);
int strncmp(const char* s1, const char* s2, size_t n);
int atoi(char *p);
int stoi(char* str);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment