小记:删除字符串空格的3种方法所引发的对C library 中库函数的思考

来源:互联网 发布:sql server 2012 网盘 编辑:程序博客网 时间:2024/05/17 00:13

方法1、字符串填充方法(子函数调用)

首先创建创建一个新的字符串char str2[N]用于存储不含空格的字符,当然数字N一定要大于原字符串的大小;接着创建返回值为字符指针的函数,然后通过指针将已知字符串中的非空格字符填充至str2[N],最后在主函数中用同类型的字符指针接收输出即可达到目的。

源代码如下:

  1 /**************** Method 1:Fullfilling a new string  **************/
  2 #include <stdio.h>
  3 
  4 #define N 100
  5 
  6 char *delete(const char *str_old,char *str_new)
  7 {
  8         while(*str_old)
  9         {
 10                 if(*str_old!=' ')
 11                         *str_new++=*str_old++;
 12                 else str_old++;
 13         }       
 14         *str_new = '\0';
 15         
 16         return str_new;
 17 }       
 18 
 19 
 20 int main()
 21 {       
 22         char *str1 = "Do what you can and show your best performace!";
 23          printf("Before:%s\n",str1); 
 24         char str2[N];
 25          
 26         delete(str1,str2);
 27          printf("After:%s\n",str2);
 28         return 0;
 29  }      
 30  

  

程序运行结果如下:


方法2:利用动态内存分配

 33 /******************Method 2: Dynamic memory allocation **********************/
 34 #include <stdio.h>
 35 #include <malloc.h>
 36 #include <string.h>
 37 
 38 char *delete(const char *str_old)
 39 {
 40         int i = 0;
 41         int n = strlen(str_old);
 42         char* str_new = malloc(n);
 43 //      printf("%s\n",str_new); // the default value is all empty!
 44 //      while(n--)
 45 //      printf("%d\n",*(str_new+n));
 46 // When allocating memory by malloc(),the default value after allocated is zero.So the char* str_new is include the end sign of charater string('\0');
 47         while(*str_old)
 48         {
 49                 if(*str_old!=' ')
 50                 *(str_new+i++) = *str_old++;
 51                 else
 52                 str_old++;
 53         }
 54         return str_new;
 55 }
 56 
 57 
 58 int main()
 59 {
 60         char *str1 = "Do what you can and show your best performace!";
 61         printf("Before:%s\n",str1);
 62         char *str = delete(str1);
 63         printf("After:%s\n",str);
 64         free(str);
 65         return 0;
 66 }
 67 

运行结果如下:


方法3:利用自建的字符串拷贝函数 

注意:内置的拷贝函数strcpy(char* des,const char* src)无法达到效果!

原因:见代码后面解释。

源代码如下

  1 /***************Method 3: With the function of mystrcpy() **************************/
  2 
  3 #include <stdio.h>
  4 #include <string.h>
  5 
  6 void mystrcpy(char *str_des,char* str_sou)
  7 {       
  8         while(*str_sou)
  9                 *str_des++ = *str_sou++;
 10         *str_des = '\0';
 11 }
 12 
 13 char *space_delete(char *str)
 14 {       
 15         int n = strlen(str),n1 = 0;
 16         while(*str)
 17         {       
 18                 if(*str == ' ')
 19                 {       
 20                         n1++;
 21                         mystrcpy(str,str+1);
 22                 }
 23                 else
 24                 str++;
 25         }
 26         return str-n+n1;
 27 }
 28 
 29 int main()
 30 {       
 31         char str[] = "Do what you can and show your best performance!";
 32 //      printf("address of str :%\n",&str);
 33         printf("Before:%s\n",str);
 34         char* str1 = space_delete(str);
 35 //      printf("address of str1:%x\n",&str1);
 36         printf("After:%s\n",str1);
 37         
 38         return 0;
 39 }

结果如下:


屏蔽掉自建的strcpy()函数,将第21行改为strcpy(str,str+1)后,运行结果如下:

仔细观察可以看到,what单词中的a被丢失了,同时your后面的r也没了。。。连续后面的几个均出现很难解释的错误。推断:strcpy()在C library中的复制过程并不是逐个字符进行复制的,详细原因还得看下库函数源代码才能清晰晓得! 

经过对strcpy()使用手册的查询知道:“ The  strings  may  not overlap, and the destination string dest must be large enough to receive the copy.”,意思是说strcpy()中的两个参数不能对同一字符串进行操作,进行strcpy(str,str+1)有可能会达不到预期结果!



原创粉丝点击