//
// main.c
// string.h
//
// Created by lichan on 13-11-21.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
int mystrlen(const char *str)
{
char *cp = (char *)str;
int length = 0;
while(*cp)
cp++,length++;
return length;
}
char * mystrcpy(char *dest,const char* source)
{
if (NULL == dest || NULL == source) {
return NULL;
}
char *address = dest;
char *st = (char *)source;
while ((*address++ = *st++))
;
*address = '\0';
return dest;
}
char *mystrcat(char *dest,const char *source)
{
if (NULL == dest || NULL == source) {
return NULL;
}
char *address =dest;
if (!*source)
return dest;
char *cs = (char *)source;
while (*dest)
dest++;
;
while ((*dest++ = *cs++))
;
return address;
}
char *mystrstr(const char *dest,const char* source)
{
if (NULL == dest || NULL == source) {
return NULL;
}
char *cp = (char *)dest;
if (!*source)
return cp;
while (*cp) {
char *str1 = cp;
char *str2 = (char *)source;
while (*str1 && *str2 && !(*str1 - *str2))
str1++,str2++;
if (!*str2)
return cp;
cp++;
}
return NULL;
}
int mystrcmp(const char* dest,const char*source)
{
//1 >; 0== ;-1<
// if (NULL == dest || NULL == source) {
// printf("输入的参数地址不正确.\n");
// return 2;
// }
char *str1 = (char *)dest;
char *str2 = (char *)source;
while (*str1 == *str2)
{
if (*str1 == '\0') {
return 0;
}
str1++,str2++;
}
return *str1-*str2;
}
int main(int argc, const char * argv[])
{
char *str1 = "lichan";
printf("%d",mystrlen(str1));
printf("\n---------------\n");
char *dest2 = malloc(sizeof(char)*30);
char *source2 = "lichan";
printf("strcpy: %s",mystrcpy(dest2, source2));
printf("\n---------------\n");
char dest3[30] = "lichan";
char *source = " nihao!";
printf("strcat: %s",mystrcat(dest3, source));
printf("\n---------------\n");
char *dest4 ="lichan";
char *source4 = "sch";
printf("strstr:%s",mystrstr(dest4, source4));
char *dest5 = "lichan";
char *source5 = "licshan";
printf("strcmp: %d",mystrcmp(dest5, source5));
printf("\n---------------\n");
printf("\n---------------\n");
printf("\n---------------\n");
printf("\n---------------\n");
printf("\n---------------\n");
// insert code here...
printf("Hello, World!\n");
return 0;
}