/*************************************************************************
> File Name: class.c
> Author: khalil
> Mail: hsgwpj@gmail.com
> Created Time: Sun 08 Nov 2015 09:13:06 AM CST
************************************************************************/
//字符串操作
//Zabbix
//开源中国的开源榜
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#if 0
void *Malloc(size_t size);
//char *buff = (char *)malloc(1024 * 1024 * 300);
//预支内存
void *Malloc(size_t size)
{
void *result = NULL;
result = malloc(size);
if (result == NULL){
//free(buff); //如果申请失败释放内存 加大申请成功率
//result = malloc(size); //然后再申请
if (result == NULL){
fprintf(stderr,"The memory is full!\n");
exit(1);
}
}
bzero(result,size);
return result;
}
int main(int argc, char **argv)
{
char *str1 = "hello,world"; //case1
char str2[100] = "hello,world"; //case2
char str3[] = "hello,world"; //case3
char *str4 = NULL;
char a = '0';
// str4 = (char *)malloc(20);
// if (str4 == NULL){
// fprintf(stderr,"The memory is full!\n");
// exit(1);
// }
str4 = (char *)Malloc(20);
strcpy(str4,"hello,world");
return 0;
}
#endif
#if 1
size_t my_strlen(const char *string); // 字符串长度
//长度不受限
char *my_strcpy(char *des_str, const char *src_str); //字符串拷贝
char *my_strcat(char *des_str, const char *src_str); //字符串链接
int my_strcmp(const char *string1, const char *string2); //字符串的比较
//长度受限
//从src_str中拷贝num个字符到des_str中
char *my_strncpy(char *des_str, const char *src_str, size_t num);
//从src_str中追加num个到des_str中
char *my_strncat(char *des_str, const char *src_str, size_t num);
//string1和string2比较num个字符
int my_strncmp(const char *s1_str, const char *s2_str, size_t num);
char *my_strchr(const char *s, int c);
char *my_strrchr(const char *s, int c);
char *my_strstr(const char *s1, const char *s2);
int find_index(const char *s, int c); //查找下标
size_t my_strpbrk(const char *str, const char *accept); //匹配str中字母,返回首次失配前匹配到的字符
size_t my_strcspn(const char *str, const char *reject); //和上一个正好相反 匹配失配的字符个数
int my_tolower(int ch);
int my_toupper(int ch);
size_t my_strlen(const char *string) // 字符串长度
{
char *str = (char *)string;
size_t len = 0;
while(str[len++] != '\0');
return len;
}
char *my_strcpy(char *des_str, const char *src_str) //字符串拷贝
{
char *des = des_str;
char *src = (char *)src_str;
while (*src != '\0'){
*des++ = *src++;
}
return des_str;
}
char *my_strcat(char *des_str, const char *src_str) //字符串链接
{
char *des = des_str;
char *src = (char *)src_str;
while(*des++);
des--;
while (*des++ = *src++);
return des_str;
}
int my_strcmp(const char *s1_str,const char *s2_str)
{
assert(s1_str != NULL && s2_str != NULL);
if( s1_str == s2_str )
{
return 0;
}
char * s1 = (char *) s1_str;
char * s2 = (char *) s2_str;
while(*s1 == *s2)
{
s1++;
s2++;
}
if (*s1 == *s2){
return 0;
}else{
return *s1-*s2;
}
}
char *my_strncpy(char *des_str, const char *src_str, size_t num) //字符串拷贝
{
char *des = des_str;
char *src = (char *)src_str;
while (num-- && *src != '\0'){
*des++ = *src++;
}
return des_str;
}
char *my_strncat(char *des_str, const char *src_str, size_t num)
{
char *des = des_str;
char *src = (char *)src_str;
while(*des++ != '\0');
des--;
while (num -- && *src != '\0'){
*des++ = *src++;
}
*des = '\0';
return des_str;
}
int my_strncmp(const char *s1_str, const char *s2_str, size_t num)
{
if( s1_str == s2_str )
{
return 0;
}
char * s1 = (char *) s1_str;
char * s2 = (char *) s2_str;
num--;
while( num -- && *s1 == *s2 )
{
s1++;
s2++;
}
if (*s1 == *s2){
return 0;
}else{
return *s1-*s2;
}
}
char *my_strchr(const char *s, int c)
{
char *src = (char *)s;
while(*src != '\0' && *src != c && src++);
if(*src == c)
return src;
else
return NULL;
}
char *my_strrchr(const char *s, int c)
{
char *src = (char *)s;
while(*src++ != '\0');
src--;
while(*src != c && src != s && src--);
if(*src == c)
return src;
else
return NULL;
// 方法2 : 从前往后面开始找 找到时用新的把后面的替换 则到最后时返回最后一个匹配的字符
}
int find_index(const char *s, int c) //查找下标
{
char *src = (char *)s;
int index = 0;
while(*src != '\0' && *src != c && src++ && ++index);
if(*src == c)
return index;
else
return -1;
}
char *my_strstr(const char *src_str, const char *des_str)
{
char *src = (char *)src_str;
char *des = (char *)des_str;
int i = 0;
int j = 0;
for ( ; src[i] != '\0'; i++ )
{
for(j = 0 ; des[j] != '\0'; j++ )
{
if (des[j] == src[i+j])
continue;
else
break;
}
if( des[j] == '\0' )
return src + i;
}
if (src[i] == '\0')
return NULL;
}
size_t my_strpbrk(const char *str, const char *accept) //匹配str中字母,返回首次失配前匹配到的字符
{
size_t num = 0;
char *p = (char *)str;
char *a = (char *)accept;
int i = 0;
int j = 0;
for( ; p[i] != '\0'; i++ ){
for(j = 0; a[j] != '\0'; j++ )
{
if (p[i] == a[j] ){
num++;
break;
}
}
if(a[j] == '\0')
{
return num;
}
}
return 0;
}
size_t my_strcspn(const char *str, const char *reject) //和上一个正好相反 匹配失配的字符个数
{
size_t num = 0;
char *p = (char *)str;
char *a = (char *)reject;
int i = 0;
int j = 0;
for( ; p[i] != '\0'; i++ ){
for(j = 0; a[j] != '\0'; j++ )
{
if (p[i] == a[j] ){
return num;
}
}
if(a[j] == '\0')
{
num++;
}
}
return 0;
}
int my_tolower(int ch)
{
return ch - (int)('A' - 'a');
}
int my_toupper(int ch){
return ch + (int)('A' - 'a');
}
int main(int argc, char **argv)
{
char str1[] = "hello,world!";
char str2[] = "hey,dude233";
char str3[15] = {0};
char str4[] = "hellodude!";
char str5[] = "hello,";
char str6[] = "world!";
char ch = 'o';
char *result = {0};
char str7[] = "wor";
char str8[] = "eahlo";
char str9[] = "!@#,";
char ch1 = 'a';
char ch2 = 'A';
printf("strlen str1:%d\n",my_strlen(str1));
my_strcpy(str3,str1);
printf("strcpy str3:%s\n",str3);
printf("str2:%s\n",str2);
my_strcat(str3,str2);
printf("str2:%s\n",str2);
printf("strcat str3:%s\n",str3);
printf("strcmp str2-str1:%d\n",my_strcmp(str2,str1));
my_strncpy(str2,str1,5);
printf("strncpy str2 str1 5:%s\n",str2);
my_strncat(str4,str1,5);
printf("strncat str4 str1 5:%s\n",str4);
printf("strncmp str2 str1 2:%d\n",my_strncmp(str4,str1,5));
result = my_strchr(str1,ch);
printf("strchr str1 o:%s\n",result);
result = my_strrchr(str1,ch);
printf("strrchr str1 o:%s\n",result);
printf("find_index str1 o:%d\n",find_index(str1,ch));
printf("strstr str1 str7:%s\n",my_strstr(str1,str7));
printf("strpbrk str1 str8:%u\n",my_strpbrk(str1,str8));
printf("strcspn str1 str9:%u\n",my_strcspn(str1,str9));
printf("a to A:%c\n",my_toupper(ch1));
printf("A to a:%c\n",my_tolower(ch2));
return 0;
}
#endif
#if 0
void *my_memmove(void *dest, const void *src, size_t n);
void *my_memmove(void *dest, const void *src, size_t n)
{
char *d = (char *)dest;
char *s = (char *)src;
if (dest == src || dest == NULL || src == NULL){
return dest;
}
if ( d < s )
while( n-- ){
*d++ = *s++;
}
else{
while( n-- ){
d[n] = s[n];
}
}
return dest;
}
int main(int argc, char **argv)
{
char str1[] = "hello,world";
char *str2 = str1 + 5;
char *str3 = str1 - 1;
str2 = my_memmove(str2,str1,12);
str3 = my_memmove(str3,str1,12);
printf("str2:%s\n",str2);
printf("str1:%s\n",str1);
printf("str3:%s\n",str3);
return 0;
}
#endif