zoukankan      html  css  js  c++  java
  • [c/c++] programming之路(23)、字符串(四)——strncat,atoi,strcmp,strlen等,以及常用内存函数

    一、strncat及自行封装实现

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>    //<string.h>是C版本的头文件,包含比如strcpy、strcat之类的字符串处理函数。
                        //<cstring>是C++版本的<string.h>
                        //<string>定义了一个string的字符串类,包含了string类的各种操作,如s.size(), s.erase(), s.insert()等。
                        //但<string>又包含了老的C版本的字符串操作如strcpy、strcat等
    
    void main() {
        char str[30] = "yincheng";
        char str1[20] = "8848.88";
        strncat(str, str1, 4);//从str1中拷贝4个字节到str
        printf("%s
    ", str);
        system("pause");
    }

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    
    void mystrncat(char *bc, char *c, int length)
    {
        if (bc == NULL || c == NULL || length == 0)
            return;
        char *p = bc;
        while (*p)
            p++;
        //循环结束后,p指向了字符串的末端''
        for (int i = 0; i < length; i++)
        {
            *p = c[i];
            p++;
        }
        *p = '';
    }
    
    void main() {
        char str[30] = "task";
        char str1[20] = "list8848.88";
        mystrncat(str, str1, 4);
        printf("%s
    ", str);
        system(str);
        system("pause");
    }

    二、atoi

    #include<stdio.h>
    #include<stdlib.h>
    
    void main() {
        char str[10] = "88e48";
        //int num = atoi(str);//8848
        //int num = atoi(str+1);//848
        int num = atoi(str);//8848
    
        char s1[10] = "88e48";
        //int n1 = atoi(s1);//88
        int n1 = atoi(s1+3);//48
    
        char s2[10] = "e8848";
        //int n2 = atoi(s2);//0
        int n2 = atoi(s2 + 1);//8848
    
        printf("%d
    ", n2);
        system("pause");
    }

     三、strrev(字符串逆转)

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void mystrrev(char *p) {
        int length = strlen(p);//获取字符串长度
        for (int i = 0; i < length/2; i++)//length必须除以2,不然字符对调之后又调回去了
        {
            char temp = p[i];
            p[i] = p[length - 1 - i];
            p[length - 1 - i] = temp;
        }
    }
    
    void main() {
        char str[20] = "yincheng8848a";
        printf("原来字符串 %s
    ", str);
        //_strrev(str);
        mystrrev(str);
        printf("后来字符串 %s
    ", str);
        system("pause");
    }

     四、strupr和strlwr(大小写转换)

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void tobig(char *p) {
        while (*p)
        {
            if ((*p)>= 'a'&&(*p) <= 'z')
                *p -= 32;
            p++;
        }
    }
    
    
    void main() {
        char str[20] = "notepad";
        //_strupr(str);//小写转大写
        //toupper用法;    #include<cctype>    ch = toupper(ch);//ch是一个字符
        tobig(str);
        printf("%s
    ", str);
    
        char str1[20] = "TASKLIST";
        _strlwr(str1);//大写转小写
        printf("%s
    ", str1);
    
        system("pause");
    }

     五、strlen

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int mystrlen(char *p) {
        int i = 0;
        while (*p) {
            i++;
            p++;
        }
        return i;
    }
    
    
    void main() {
        char str[100] = "i love iphone";//str是变量
        char *p = "i love china";//常量
        int len1 = strlen(str);
        int len2 = mystrlen(p);    
    
        printf("str=%d,p=%d
    ",len1,len2);
    
        system("pause");
    }

     六、strcat(字符串拼接)

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    char * mystrcpy(char *dst,char *src) {
        char *p = dst;
        if (dst == NULL || src == NULL)
            return NULL;
        while (*src) {
            *dst = *src;
            dst++;
            src++;
        }
        *dst = '';
        return p;
    }
    
    char * mystrcat(char *dst, char *src) {
        char *p = dst;
        if (dst == NULL || src == NULL)
            return NULL;
        while (*dst) 
            dst++;
        while (*src) {
            *dst = *src;
            dst++;
            src++;
        }
        *dst = '';
        return p;
    }
    
    
    
    void main() {
        char str1[10] = "note";
        char str2[10] = "pad";
        char str[20];
        printf("%s
    ", mystrcpy(str, str1));
        //mystrcpy(str, str1);//复制
        printf("%s
    ", str);
        //strcat(str, str2);//拼接
        mystrcat(str, str2);//拼接
        printf("%s
    ", str);
        system(str);
    }
    
    void main0() {
        char str1[10] = "note";
        char str2[10] = "pad";
        char str[20];
        sprintf(str, "%s%s", str1, str2);
        system(str);
        system("pause");
    }

     七、strchr(查找某字符是否在字符串中)

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    char * mystrchr(char *dst,char ch) {
        if (dst == NULL)
            return NULL;
        while (*dst) {
            if (*dst == ch) 
                return dst;
            dst++;
        }
        return NULL;
    }
    
    
    void main() {
        char str[20] = "i love china";
        char ch='o';
        /*char *p = strchr(str, ch);*/
        char *p = mystrchr(str, ch);
        if(p==NULL)
            printf("没找到
    ");
        else
            printf("值%c,地址%x
    ",*p,p);
    
        system("pause");
    }

     八、strcmp(比较两字符串是否相等)

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int mystrcmp(char *p1,char *p2) {
        int l1 = strlen(p1);
        int l2 = strlen(p2);
        if (l1 != l2)
            return -1;
        else {
            for (;*p1;p1++,p2++)
            {
                if (*p1 != *p2)
                    return -1;
            }
        }
        return 0; 
    }
    
    void main() {
        char str1[10] = "note";
        char str2[10] = "note";
        /*if (strcmp(str1,str2)==0)*/
        if (mystrcmp(str1, str2) == 0)
            printf("相等
    ");
        else
            printf("不相等
    ");
        system("pause");
    }
    
    void main0() {//无论相等还是不等,都输出不等
        char str1[10] = "note";
        char str2[10] = "note";
        if(str1==str2)//c语言判断字符串不能这样
            printf("相等
    ");
        else
            printf("不相等
    ");
        system("pause");
    }

     实现排序

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void main() {
        char str[10][20] = {"apple","google","IBM","Intel","Microsoft","baidu","ali","tencent","360","Sun"};
        printf("排序之前
    ");
        for (int i = 0; i < 10; i++)
            printf("%s
    ", str[i]);
        printf("排序之后
    ");
        //冒泡排序
        for (int i = 0; i < 10-1; i++)
        {
            for (int j = 0; j < 10 - 1 - i; j++)
            {
                if (strcmp(str[j], str[j+1]) > 0) {
                    //交换字符串,不可以用赋值号
                    char temp[30];
                    strcpy(temp, str[j]);
                    strcpy(str[j],str[j+1]);
                    strcpy(str[j+1],temp);
                }
            }
        }
        for (int i = 0; i < 10; i++)
        {
            printf("%s
    ", str[i]);
        }
    
        system("pause");
    }

    八、strstr(在字符串中查找是否包含另一个字符串)

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    char * mystrstr(char *allstr,char *str) {
        if (allstr == NULL || str == NULL)
            return NULL;
        int all_len = strlen(allstr);
        int str_len = strlen(str);
        for (int i = 0; i <all_len-str_len+1; i++)
        {
            int flag = 1;//标示,假定字符串一开始相等
            for (int j = 0; j <str_len; j++)
            {
                if (allstr[i + j] != str[j]) {
                    flag = 0;
                    break;
                }
            }
            if (flag)
                return allstr+i; //返回找到的地址
        }
        return NULL;
    }
    
    void main() {
        char allstr[40] = "i love china i love cpp";
        char str[10] = "china";
        //char *p = strstr(allstr, str);//检索
        char *p = mystrstr(allstr, str);//检索
        if(p==NULL)
            printf("没找到
    ");
        else
            printf("值%c,地址%x
    ",*p,p);
        system("pause");
    }

     九、常用内存函数

    1.memset

    #include<stdio.h>
    #include<stdlib.h>
    #include<string>
    #include<memory.h>//内存操作函数
    
    void main() {
        char str[40] = "i love china i love c i love cpp";
        //第一个参数是内存的首地址,第二个参数是要设置的字符
        //第三个参数是整数,从首地址开始前进多少字节,把这一段设置为该字符
        memset(str, 'A', 10);
        printf("%s
    ",str);
        memset(str, 'A', 12);
        printf("%s
    ", str);
        memset(str, '',strlen(str));//清空字符串
        printf("%s
    ", str);
        system("pause");
    }

    2.memcpy

    #include<stdio.h>
    #include<stdlib.h>
    #include<string>
    #include<memory.h>//内存操作函数
    
    void main0() {
        char str[30] = "yincheng8848";
        char str1[20] = "hello c";
        printf("%s
    ", str);
        //memcpy:从地址str开始,从str1处拷贝5个字节到str
        memcpy(str,str1,5);
        printf("%s
    ",str);
        
        system("pause");
    }
    
    void main() {
        int a[] = { 1,3,5,7,9 };
        int b[] = { 0,2,4,6,8 };
        memcpy(a,b, 8);//每个元素4个字节,两个元素8个字节
        //memcpy按照内存字节来拷贝,不管是什么类型,都是拷贝二进制类型
        for (int i = 0; i < 5; i++)
        {
            printf("%d
    ",a[i]);
        }
        system("pause");
    }

    3.memccpy

    #include<stdio.h>
    #include<stdlib.h>
    #include<string>
    #include<memory.h>//内存操作函数
    
    void main() {
        char str[30] = "yincheng8848";
        char str1[20] = "hello c";
        printf("%s
    ", str);
        _memccpy(str,str1,'l',7);
        //memccpy:如果str1没有字符‘l’,就等价于memcpy,读取7个字符拷贝到str
        //如果有,读取拷贝到该字符就停止
        printf("%s
    ",str);
        
        system("pause");
    }

    4.memchr

    #include<stdio.h>
    #include<memory.h>
    
    void main() {
        char str[30] = "helloyincheng";
        char ch='c';
        char *p =(char *) memchr(str, 'c', 30);
        //从地址str开始,前进30个字节检索
        //如果存在,返回地址,否则返回空
        if (p == NULL)
            printf("没找到
    ");
        else
            printf("找到字符%c,地址%p", *p, p);
        getchar();
    }

    5.memicmp

    #include<stdio.h>
    #include<stdlib.h>
    #include<memory.h>
    
    void main() {
        char *buf1 = "ABCDE123";
        char *buf2 = "abcde456";
        int num = _memicmp(buf1, buf2, 5);
        //memicmp判断字符串前面几个字节是否相等
        //忽略大小写,相等返回0,反之非0
        if (num == 0)
            printf("忽略大小写的情况下,相等");
        else
            printf("不等");
        
        getchar();
    }

  • 相关阅读:
    declare handler 声明异常处理的语法
    mysql存储过程获取sqlstate message_text
    mongoTemplate操作内嵌文档
    mysql索引之七:组合索引中选择合适的索引列顺序
    mongoDB的操作总结
    explain之三:MYSQL EXPLAIN语句的extended 选项学习体会,分析诊断工具之二
    状态模式
    代码重构----使用java有限状态机来消除太多的if else判断
    断路器(CircuitBreaker)设计模式
    断路器之一:Hystrix 使用与分析
  • 原文地址:https://www.cnblogs.com/little-monkey/p/7526509.html
Copyright © 2011-2022 走看看