zoukankan      html  css  js  c++  java
  • 字符串指针

    #include <stdio.h>
    #include <string.h>
    void primaryPart(void);
    void myStrcpy(char *destination, const char *resource);
    void myAStrcpy(char *destination, const char *resource);
    void myStrncpy(char *destination, const char *resource, int length);
    int characterCount(const char *);
    void primaryCount(void);
    int myStrcmp(const char *, const char *);
    void Myitoa(int, char *);
    
    int main(){
    
        //{
        //    //primaryPart();
        //    char buf1[10];
        //    char *p = "nihaoa";
        //    char buf2[] = "hellosssssssss";
        //    //myStrncpy(buf1, buf2,6);
        //    //printf("%s
    ", buf1);
        //    char chinese2[] = "我s爱s你";
        //    //printf("%s 的字符个数有:%d", chinese2, characterCount(chinese2));
        //}
    
    
    
        //{
        //    char s1[] = "bbc";
        //    char s3[] = "abc";
        //    char s2[] = "ab";
        //    char s[] = "";
        //    //printf("%d", myStrcmp("", ""));
        //    //printf("%d", 'b'-'c');
        //    *s == 0;
        //    //printf("%d", *s==0);//这里要加载符号
        //    printf("%d", myStrcmp("", s1));
        //}
    
        char s[4];
        Myitoa(123, s);
        printf("%s
    ", s);
    
    
        getchar();
        return 0;
    
    }
    
    
    /** 
        废弃掉,重新写一个。
    */
    void Myitoa(int number, char *myint){
    
        char numCharacter[10] = { "123456789" };
        int time = 0;
        while (number / 10 != 0){
            *(myint + time++) = numCharacter[number % 10];
            number = number / 10;
        }
    
    
    }
    /**
        字符串比较.
        库里面也没有比较 NULL的情况
        测试用例不是很全,大概就是这个样子了。再有问题在修改吧。这种问题一般都会拖到无穷远。。。
    */
    int myStrcmp(const char *str1, const char *str2){
    
        /*int result = *str1++ - *str2++;
        while (result == 0){
            result = *str1++ - *str2++;
        }*/
        if (*str1 == 0 && *str2 == 0){
            return 0;
        }
        int result = 0;
        while ((result=*str1++ - *str2++ )== 0){
            
        }
        return result;
    }
    
    /** 
        前情提要
    */
    void primaryCount(void){
        char chinese[] = "我爱你";
        char chinese2[] = "我s爱s你";
        printf("%s
    sizeof(chinese)=%d
    strlen(chinese)=%d
    ", chinese, sizeof(chinese), strlen(chinese));//前面是7 后面是6.
        printf("%s
    sizeof(chinese2)=%d
    strlen(chinese2)=%d
    ", chinese2, sizeof(chinese2), strlen(chinese2));//前面是9 后面是8.
        //这样就不太好了,我们需要的是计数,一个字符算一个数儿。汉字是两个字节,英文字是1个字节。所以记录的结果是这样的。
    }
     
    /** 
        汉字是两个字节,英文字是1个字节。
        希望一个汉字 或者 一个英文计数都只加1
    
    这个 需要配一个图
    
        这个代码绝对牛逼!!!全文亮点了,至少因为 我能看懂,并且也写得出来
    */
    int characterCount(const char *p){
        int result = 0;
        while (*p != 0){
            result++;
            if (*p++ < 0){
                p++;
            }
        }
        return result;
    }
    
    /**
        这个 不想改,这个 又好看懂并且执行也不慢。就这样了!!!不改了!!!一定要用最简单的方式 实现最复杂的功能!!!
        避免溢出
    */
    void myStrncpy(char *destination, const char *resource, int length){
    
        for (int i = 0; i < length; i++){
            *destination++ = *resource++;
        }
        *destination = 0;
    }
    /** 
        比strcpy高级一些的写法
    */
    void myAStrcpy(char *destination, const char *resource){
    
        while (*destination++ = *resource++);
    
    }
    
    /** 
        稍微 比较容易理解的拷贝方式
    */
    void myStrcpy(char *destination, const char *resource){//会有溢出错误
    
        while (*resource != ''){//这里 0跟 的效力是相同的
            *destination++ = *resource++;
        }
        *destination = 0;
    }
    
    void primaryPart(void){
    
        char s[] = "hello world";
        printf("sizeof(s)=%d
    strlen(s)=%d
    ", sizeof(s), strlen(s));//两者在数值上相差1,sizeof因为 要保证结尾所以多一个字符的空间
        //sizeof本质上是这段内容在内存中所占据的空间大小,而,strlen是表示这段内容中,纯字符的部分的大小。
        char *p = &s;
        //p[3] = 'a';
        *(p + 3) = 'a';
        printf("%s
    ", s);
    
        char *p1 = "hello world";
        *(p1 + 3) = 'a';//虽然编译可以过,但是这里会出现问题。
        printf("%s
    ", p1);
    
    }
  • 相关阅读:
    三大主流负载均衡软件对比(LVS+Nginx+HAproxy)
    nginx 提示the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
    centos安装nginx并配置SSL证书
    hadoop创建目录文件失败
    The server time zone value 'EDT' is unrecognized or represents more than one time zone.
    脚本启动SpringBoot(jar)
    centos做免密登录
    数据库远程连接配置
    Bash 快捷键
    TCP三次握手四次断开
  • 原文地址:https://www.cnblogs.com/letben/p/5229841.html
Copyright © 2011-2022 走看看