• C基础--字符串操作函数(strlen,strcpy,strcmp,strcat,strstr,strtok,strchr)


    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
        //char dest[10];
        //#define NULL (void *)0
        //char *dest = NULL;            //dest是野指针,指向的区域没有可读写空间
        //char dest[3];
        
        //char src[] = "hello";
        char str1[10] = "hello";
        //char str2[10] = str1;
        strcpy(str2, str1);
        printf("%s
    ", str2);
    
        //printf("%s
    ", strcpy(dest, src));
        //printf("%s
    ", strncpy(dest, src, sizeof(dest)));
    
        return 0;
    }
    #include <stdio.h>
    #include <string.h>
    hello
    helloworld
    strncmp("hello", "helloworld", 5);
    int main(void)
    {
        int i;
        char name[][10] = {"xiaoming", "itcast", "xiaoqiang", "xiaohong"};
        char key[10];
        /*
        char a[10] = "xiaoming";    //name[0]  == > a
        char b[10] = "itcast";
        char c[10] = "xiaoqiang";
        char d[10] = "xiaohong";
        */
        //char name[][10] = {"xiaomingitcastxiaoqiangxiaohong"};
    
        for (i = 0; i < 4; i++) 
        {
            if (strcmp(name[i], "xiaohong") == 0) {
                printf("%d	%s
    ", i, name[i]);
                strcpy(key, name[i]);
            }
        }
        printf("%s
    ", key);
        
        return 0;
    }
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
        //char s1[10] = "hello";    
        //char *s1 = "hello";    
        char s1[7] = "hello";   //注意S1要有足够的空间放拼接后的字符串,不然发生数组越界 
        char s2[] = "BOY";
    
        strcat(s1, s2);
    
        printf("%s
    ", s1);
        
        return 0;
    }
    #include <stdio.h>
    #include <string.h>
    int main(int argc, char *argv[])
    {
        char str[] = "hello world itcast";
        char key;
        printf("%s
    ", str);
        key = getchar();
        char *p = NULL;
        p = strchr(str, key);
    
        (!p)?printf("un find
    "):printf("%s
    ", p);
    
        return 0;
    }
    #include <stdio.h>
    #include <string.h>
    /*
    int main(void)
    {
        char str[] = "hello world itcast good";
        char *p;
    
        p = strtok(str, " ");
        printf("%s
    ", p);
        while ((p = strtok(NULL, " ")) != NULL)
            printf("%s
    ", p);
    //    p = strtok(str, " ");
    
        //printf("str = %s
    ", str);
    
        return 0;
    }
    */
    /*
    int main(void)
    {
        char str[] = "hello, world, itcast! good.; night?";
        char *dlim = " ,?!.;";
        //char dlim[] = ".?!.;";
        char *p;
    
        p = strtok(str, dlim);
        printf("%s
    ", p);
        while ((p = strtok(NULL, dlim)) != NULL)
            printf("%s
    ", p);
    //    p = strtok(str, " ");
    
        //printf("str = %s
    ", str);
    
        return 0;
    }
    */
    size_t strlen(const char *str)
    {
        return 6;
    }
    int main(void)
    {
        char str[] = "hello world itcast good night";
        char tmp[] = "I am a student
    ";
        char *dlim = " ";
        char *p;
        printf("%d
    ", (int)strlen(tmp));
    
        p = strtok(str, dlim);  //内部指针保存下一次要strtok的字符串地址
        printf("%s
    ", p);
    
        p = strtok(tmp, dlim);
        printf("%s
    ", p);
    
        while ((p = strtok(NULL, dlim)) != NULL)
            printf("%s
    ", p);
    
        return 0;
    }
    #include <stdio.h>
    
    int a = 3;
    int b;
    
    int main(void)
    {
        int c = 3;
        char *str1 = "hello";     //"hello"属于字符串常量,存储在只读数据区
        char str2[]  = "hello";  //str2[6] 内存的栈上分配,栈的属性可读可写
        printf("%p
    ", &a);
        printf("%p
    ", &b);
        printf("%p
    ", &c);
        printf("%p
    ", str1);
        printf("%p
    ", &str1);
        printf("%p
    ", str2);  //str2+1
        printf("%p
    ", &str2);// &str2+1
    
        return 0;
    }
  • 相关阅读:
    chapter01
    2019.07.11
    系统进程
    Linex第五-第七章
    Linex第三章第四章
    Linux 系统管理 第二章第三章
    2019/7/24
    使用.htaccess进行浏览器图片文件缓存
    div+css3实现漂亮的多彩标签云,鼠标移动会有动画
    搜索排序的作弊与反作弊,面壁人与智子的巅峰对决
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4824253.html
Copyright © 2020-2023  润新知