zoukankan      html  css  js  c++  java
  • 字符串典型问题分析

    典型问题一

    下面的程序输出什么?为什么?

    #include<stdio.h>
    
    int main()
    {
    	char buf[10] = {0};
    	char src[] = "hello %s";
    	snprintf(buf,sizeof(buf),src);
    	printf("buf = %s
    ",buf);
    
    	return 0;
    }
    
    

    分析:
    snprintf()函数本身是可变参数函数,原型如下:
    int snprintf(char* buffer,int buf_size,const char* formart,...)
    注意:当函数有3个参数时,如果第三个参数没有包含格式化信息,函数调用没有问题;相反,如果第三个参数包含了格式化信息,但缺少后续相应参数,则程序行为不确定。

    典型问题二

    下面的程序输出什么?为什么

    #include <stdio.h>  
    #include <string.h>  
      
    int main()  
    {  
        #define STR "Hello, hello"  
          
        char* src = STR;  
        char buf[255] = {0};  
          
        snprintf(buf, sizeof(buf), src);  
          
        printf("strlen(STR) = %d
    ", strlen(STR));  
        printf("sizeof(STR) = %d
    ", sizeof(STR));  
          
        printf("strlen(src) = %d
    ", strlen(src));  
        printf("sizeof(src) = %d
    ", sizeof(src));  
          
        printf("strlen(buf) = %d
    ", strlen(buf));  
        printf("sizeof(buf) = %d
    ", sizeof(buf));  
          
        printf("src = %s
    ", src);  
        printf("buf = %s
    ", buf);  
          
        return 0;  
    }  
    

    分析:

    • 字符串相关的函数均以第一个出现的''作为结束符
    • 编译器总是会在字符串字面量的末尾添加''
    • 字符串字面量的本质为数组

    典型问题三

    下面的程序输出什么,为什么?

    #include <stdio.h>  
    #include <string.h>  
      
    int main()  
    {  
        #define S1 "HelloWorld"  
        #define S2 "HelloWorld"  
          
        if( S1 == S2 )  
        {  
            printf("Equal
    ");  
        }  
        else  
        {  
            printf("Non Equal
    ");  
        }  
          
        if( strcmp(S1, S2) == 0 )  
        {  
            printf("Equal
    ");  
        }  
        else  
        {  
            printf("Non Equal
    ");  
        }  
          
        return 0;  
    }  
    

    分析:

    • 字符串之间的相等比较需要用strcmp完成
    • 不可以用==进行字符串直接的比较
    • 完全相同的字符串字面量的==比较结果为false

    注:一些现代编译器能够将相同的字符串字面量映射到同一个无名字符数组,因此==比较结果为true

    典型问题四

    字符串循环右移

    #include <stdio.h>  
    #include <string.h>  
      
    void right_shift_r(const char* src, char* result, unsigned int n)  
    {  
        const unsigned int LEN = strlen(src);  
        int i = 0;  
              
        for(i=0; i < LEN; i++)  
        {  
            result[(n + i) % LEN] = src[i];  
        }  
          
        result[LEN] = '';  
    }  
      
    int main()  
    {  
        char result[255] = {0};  
          
        right_shift_r("abcde", result, 2);  
          
        printf("%s
    ", result);  
          
        right_shift_r("abcde", result, 5);  
          
        printf("%s
    ", result);  
          
        right_shift_r("abcde", result, 8);  
          
        printf("%s
    ", result);  
          
        return 0;  
    }  
    
  • 相关阅读:
    字符串格式化及操作操作
    操作文件
    python学习笔记(数据类型)
    抓包工具之—charles碎言碎语
    jmeter之关联操作
    eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
    java使用this关键字调用本类重载构造器
    无法安装Windows Live“OnCatalogResult:0x80190194”错误的解决方法
    JavaScript笔试必备语句
    VS2015详细安装步骤
  • 原文地址:https://www.cnblogs.com/yanyun888/p/9213181.html
Copyright © 2011-2022 走看看