strcpy strlen memcpy strcat strcmp strstr strrev函数的实现代码

/* 
//strcpy函数的实现,注意命名要与原来的库函数有区别 
#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;

char *mystrcpy(char *strDest, const char *strSrc)
{
     if ((strDest == NULL) || (strSrc == NULL))//保证strDest和strSrc的有效性 
         return NULL;
     char *strDestCopy = strDest;//保存目标字符串的首地址,以便最后的返回 
     //注意:++的优先级要比*的要高 
     while ((*strDest++ = *strSrc++) != '\0');//将字符从strsrc中复制到strDest中 
     return strDestCopy; 
}

int main()
{
    char *strSrc = "Hello World!";
    char strDest[20];
    mystrcpy(strDest, strSrc);
    printf("strDest:%s\n", strDest); 
    
    system("pause");
}
*/

/* 
//strlen函数的实现,有两种方法实现
#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;

//第一种方法就是通过一个变量len的自增,直至遇到字符串的结束字符 
int mystrlen1(const char *strSrc)
{
    assert(strSrc != NULL);//保证字符串首地址的有效性 
    int len = 0;//结果的保存 
    while (*strSrc++ != '\0'){
          len++; 
    } 
    return len; 
} 

//第二种方法就是通过对字符串首地址的保存,再用字符串的末地址减去首地址就可以
int mystrlen2(char *strSrc)
{
    int len; 
    char *tmpstrSrc = strSrc;//将字符串的首地址先保存下来 
    while (*strSrc++ != '\0'); //直至遇到字符串的结束符,strSrc已经为字符串的末地址 
    len = (strSrc - tmpstrSrc - 1);//末地址减去首地址,得出结果 
    return len; 
} 

int main()
{
    char *strSrc = "Hello World!";
    int len1, len2;
    len1 = mystrlen1(strSrc);
    len2 = mystrlen2(strSrc); 
    printf("len1 = %d\n", len1);
    printf("len2 = %d\n", len2); 
    
    system("pause"); 
} 
*/

/* 
//memcpy函数的实现 
#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;

void *mymemcpy(void *memTo, const void *memFrom, int size)
{
    assert((memTo != NULL) && (memFrom != NULL));//保证字符串的有效性
    char *tmpmemTo = (char *)memTo;//对字符串首地址的保存 
    char *tmpmemFrom = (char *)memFrom; 
    //按照size的大小进行复制 
    while (size-- > 0){
          *tmpmemTo++ = *tmpmemFrom++; 
    } 
    return memTo; 
} 

int main()
{
    char *strFrom = "Hello World!";
    char strTo[20];
    mymemcpy(strTo, strFrom, 6);
    strTo[6] = '\0';
    printf("strTo:%s\n", strTo); 
    
    system("pause"); 
} 
*/

/* 
//strcat函数的实现
#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;

char *mystrcat(char *strDest, char *strSrc)
{
     char *tmpstrDest;
     tmpstrDest = strDest;//保存目标字符串的首地址 
     while (*strDest++ != '\0');// 循环到字符串的末尾 
     strDest--; 
     while ((*strDest++ = *strSrc++) != '\0');//进行字符串的复制 
     return tmpstrDest; //字符串首地址的返回 
} 


int main()
{
    char *strDest = NULL; 
    char *strSrc1 = "Hello ";
    char *strSrc2 = "World!";
    strDest = (char *)malloc(256);
    *strDest = '\0';
    mystrcat(mystrcat(strDest, strSrc1), strSrc2);
    printf("strDest:%s\n", strDest);
    free(strDest);
    strDest = NULL; 
    
    system("pause"); 
} 
*/

/* 
//strcmp函数的实现
#include <iostream>
#include <cstdio>
using namespace std;

int mystrcmp(const char *strSrc, const char *strDest)
{
    int ans = 0;//结果的保存 
    //取两个字符串的各个字符进行相减,即是它们的ASCII相减 
    while (!(ans = *(unsigned char *)strSrc - *(unsigned char *)strDest) && *strDest){
          ++strSrc;
          ++strDest; 
    } 
    if (ans > 0)
        ans = 1;
    else if (ans < 0)
        ans = -1;
    return ans; 
} 


int main()
{
    char *str = "1234567";
    char *str1 = "1234567";
    char *str2 = "12345678";
    char *str3 = "1234566";
    
    int test1 = mystrcmp(str, str1);
    int test2 = mystrcmp(str, str2);
    int test3 = mystrcmp(str, str3);
    
    cout << test1 << endl << test2 << endl << test3 << endl; 
    
    system("pause"); 
} 
*/

/* 
//strstr函数的实现
#include <iostream>
#include <cstdio>
using namespace std;

int mystrstr(const char *strSrc, const char *strSub)
{
    int pos = 0;
    //对两个字符串的首地址进行保存 
    const char *bp; 
    const char *sp;
    if (strSrc == NULL || strSub == NULL)//保证字符串的有效性 
        return -1;
    while (strSrc){
          bp = strSrc;//得到其字符串的首地址,从首地址开始进行逐一比较字符串是否相等! 
          sp = strSub;
          do{ 
             if (!*sp)//如果子串到达了字符串的结束符,证明已经找到,返回其位置 
                return pos; 
          } while (*bp++ == *sp++);
          pos++;
          strSrc++;//如果没有找到,将主模式串的位置向前推进一位,继续比较,一直到达字符串的结尾! 
    } 
    return -1; 
} 

int main()
{
    char *strSrc = "123456";
    char *strSub = "345";
    int pos = mystrstr(strSrc, strSub);
    cout << pos << endl; 
    
    system("pause"); 
} 
*/


//strrev函数的实现 
#include <iostream>
#include <cstdio>
using namespace std;


//方法1:遍历字符串,将第一个字符和最后一个字符交换,第2个和倒数第2个字符交换,依次循环! 
char *strrev1(const char* str)
{
     int len = strlen(str);
     char *tmp = new char[len+1];
     strcpy(tmp, str);
     for (int i = 0; i < len/2; i++){
         char c = tmp[i];
         tmp[i] = tmp[len-1-i];
         tmp[len-1-i] = c; 
     } 
     
     return tmp; 
} 

//方法2:用指针进行直接的操作
char *strrev2(const char *str)
{
     int len = strlen(str);
     char *tmp = new char[len+1];
     strcpy(tmp, str);
     char *ret = tmp;//保存首地址 
     char *p = tmp + len - 1; //保存尾地址
     
     while (p > tmp){
           char t = *tmp;
           *tmp = *p;
           *p = t;
           --p;
           ++tmp; 
     } 
     
     return ret; 
} 

//方法3:我们借助异或运算(^)完成两个字符的交换
char *strrev3(const char *str)
{
     char *tmp = new char[strlen(str) + 1];
     strcpy(tmp, str);
     char *ret = tmp;
     char *p = tmp + strlen(str)-1;
     
     while (p > tmp){
           *p ^= *tmp;
           *tmp ^= *p;
           *p ^= *tmp;
           --p;
           ++tmp; 
     } 
     
     return ret; 
} 

char *strrev4(const char *str)
{
     char *tmp = new char[strlen(str) + 1];
     strcpy(tmp, str);
     char *ret = tmp;
     char *p = tmp + strlen(str)-1;
     
     while (p > tmp){
           *p = *p + *tmp;
           *tmp = *p - *tmp;
           *p = *p - *tmp;
           --p;
           ++tmp; 
     } 
     
     return ret; 
} 


int main()
{
    char *str = "123456";
    cout << str << endl;
    
    char *str2 = strrev1(str);
    cout << str2 << endl;
    
    char *str3 = strrev2(str);
    cout << str3 << endl;
    
    char *str4 = strrev3(str);
    cout << str4 << endl;
    
    char *str5 = strrev4(str);
    cout << str5 << endl;
    
    system("pause"); 
}  

你可能感兴趣的:(c,null,System)