第31课 - 字符串典型问题分析

1、典型问题一

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

  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     char buf[10] = {0};  
  6.     char src[] = "hello %s";  
  7.       
  8.     snprintf(buf, sizeof(buf), src);  
  9.       
  10.     printf("buf = %s\n", buf);  
  11.       
  12.     return 0;  
  13. }  


                    

    分析 

            snprintf函数本身是可变参数函数,原型如下: 

        int snprintf(char* buffer, int buf_size, const char*fomart, ...) 


            当函数只有3个参数时,如果第三个参数没有包含格式化信 

            息,函数调用没有问题;相反,如果第三个参数包含了格式 

            化信息,但缺少后续对应参数,则程序行为不确定。

扫描二维码关注公众号,回复: 44343 查看本文章

                                


2、典型问题二

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

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main()  
  5. {  
  6.     #define STR "Hello, \0D.T.Software\0"  
  7.       
  8.     char* src = STR;  
  9.     char buf[255] = {0};  
  10.       
  11.     snprintf(buf, sizeof(buf), src);  
  12.       
  13.     printf("strlen(STR) = %d\n", strlen(STR));  
  14.     printf("sizeof(STR) = %d\n"sizeof(STR));  
  15.       
  16.     printf("strlen(src) = %d\n", strlen(src));  
  17.     printf("sizeof(src) = %d\n"sizeof(src));  
  18.       
  19.     printf("strlen(buf) = %d\n", strlen(buf));  
  20.     printf("sizeof(buf) = %d\n"sizeof(buf));  
  21.       
  22.     printf("src = %s\n", src);  
  23.     printf("buf = %s\n", buf);  
  24.       
  25.     return 0;  
  26. }  

                    

        分析

                字符串相关的函数均以第—个出现的 '\0'作为结束符 

                编译器总是会在字符串字面量的未尾添加'\0' 

                字符串字面量的本质为数组

                    


3、典型问题三 

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

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main()  
  5. {  
  6.     #define S1 "D.T.Software"  
  7.     #define S2 "D.T.Software"  
  8.       
  9.     if( S1 == S2 )  
  10.     {  
  11.         printf("Equal\n");  
  12.     }  
  13.     else  
  14.     {  
  15.         printf("Non Equal\n");  
  16.     }  
  17.       
  18.     if( strcmp(S1, S2) == 0 )  
  19.     {  
  20.         printf("Equal\n");  
  21.     }  
  22.     else  
  23.     {  
  24.         printf("Non Equal\n");  
  25.     }  
  26.       
  27.     return 0;  
  28. }  

                    


        分析

                字符串之间的相等比较需要用strcmp完成 

                不可直接用==进行字符串直接的比较 

                完全相同的字符串字面量的==比较结果为false 


                一些现代编译器能够将相同的字符串字面溢 

                映射到同—个无名字符数组,因此==比较 

                结果为true。

                        


4、典型问题四 

        字符串循环右移



  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. void right_shift_r(const char* src, char* result, unsigned int n)  
  5. {  
  6.     const unsigned int LEN = strlen(src);  
  7.     int i = 0;  
  8.           
  9.     for(i=0; i < LEN; i++)  
  10.     {  
  11.         result[(n + i) % LEN] = src[i];  
  12.     }  
  13.       
  14.     result[LEN] = '\0';  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     char result[255] = {0};  
  20.       
  21.     right_shift_r("abcde", result, 2);  
  22.       
  23.     printf("%s\n", result);  
  24.       
  25.     right_shift_r("abcde", result, 5);  
  26.       
  27.     printf("%s\n", result);  
  28.       
  29.     right_shift_r("abcde", result, 8);  
  30.       
  31.     printf("%s\n", result);  
  32.       
  33.     return 0;  
  34. }  


                            

猜你喜欢

转载自blog.csdn.net/qq_39654127/article/details/80043071