• C++字符串操作二


    #include <iostream>
    #include <assert.h>
    using namespace std;
    //模拟实现strcmp函数。

    bool my_strcmp(const char *str1,const char *str2) { assert(str1!=NULL && str2!=NULL); const char *p = str1; const char *q = str2; while (*p != '' && *q != '' && *p == *q) { p++; q++; } return (*p - *q)!=0; } int main() { cout << my_strcmp("liwu", "liu") << endl; return 0; } #include <iostream> #include <assert.h> using namespace std; //库函数memcopy的实现。 char *my_memcopy(char *dist, const char *src, int len) { assert(dist != NULL && src != NULL); char *p = dist; int n = len>(strlen(src)+1) ?

    strlen(src)+1 : len; while (n-->0) { *dist++ = *src++; } return p; } int main() { char s1[20] = "1234"; my_memcopy(s1,"liuhuiyan",2); cout << s1 << endl; } #include <iostream> #include <assert.h> using namespace std; char* my_memove(char *dist,const char* src,int len) { assert(dist!=NULL && src!=NULL); char *p = dist; const char *q = src; int n = len>(strlen(src) + 1) ? strlen(src) + 1 : len; if (p <= q || q+n<=p) { while (n-- > 0) { *p++ = *q++; } } else { p += n-1; q += n-1; while (n-- > 0) { *p-- = *q--; } } return dist; } int main() { char s1[] = "liu hui yan"; char s2[] = "123 456"; cout << my_memove(s1, s2, 6) << endl; return 0; } #include <iostream> using namespace std; //一个字符串“student a am i”,现要求将这个字符串改动为“i am a student”。 void exchange(char *p1,char *p2) { char temp; while (p1 < p2) { temp = *p1; *p1 = *p2; *p2 = temp; p1++; p2--; } } char* my_exchange(char *src) { char *p = src; char *q = src; while (*p != '') { while (*p != ' ' && *p!='') { p++; } exchange(q,p-1); if (*p == '')break; p++; q = p; } exchange(src,p-1); return src; } int main() { char s[] = "student a am i"; cout << my_exchange(s) << endl; } //字符串打印数字。 #include <iostream> #include <assert.h> using namespace std; int my_int(const char *str) { assert(str!=NULL); int count = 0; while (*str != '') { count = (count * 10 + *str - '0'); str++; } return count; } int main() { char s[] = "12345"; cout << my_int(s) << endl; return 0; }

    #include <iostream>
    using namespace std;
    char str[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    //输入数字1234,打印对应的字符串。

    void Printf(int x) { if (x==0)return; else { Printf(x / 10); cout << str[x % 10] << endl; } } int main() { Printf(1234); return 0; }

  • 相关阅读:
    Dr.Watson
    得到当前操作系统的版本的
    屏蔽回车关闭对话框事件
    一个发邮件的类(用CDO实现)
    怎样配置SQL Server发送电子邮件
    无进程DLL木马的又一开发思路与实现
    Write AutoUpdating Apps with .NET and the Background Intelligent Transfer Service API
    WINDOWS编程入门一个初级问题的分析
    智能客户端技术总结(一)
    【.NET】C#.NET ADO.NET数据访问模型概述
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7208351.html
Copyright © 2020-2023  润新知