代码改变世界

陈彬 2019-1-21

2019-01-21 19:39  好大一个苹果  阅读(288)  评论(0编辑  收藏  举报
  问题:对*和++之间的关系运算掌握的不是太好

1 #include <stdio.h> 2 int str_lenth(char *str); 3 int str_cmp(char *str1, char *str); 4 void strcopy(char *str1, char *str2); 5 void str_cat(char *str1, char *str2,char *str3); 6 int main(void) 7 { 8 char str[] = "hello world!"; 9 char str1[] = "computer"; 10 char str2[] = "compare"; 11 char str3[] = "HELLO WORLD!"; 12 char str4[100] = {}; 13 int lenth,a; 14 lenth = str_lenth(str); 15 a = str_cmp(str1, str2); 16 strcopy(str3, str); 17 str_cat(str, str2, str4); 18 printf("字符串长度为%d\n",lenth); 19 printf("两个字符串相差%d\n",a); 20 printf("字符串的复制:%s\n",str3); 21 printf("字符串的连接:%s\n",str4); 22 return 0; 23 } 24 /* 25 函数功能:求字符串长度 26 */ 27 int str_lenth(char *str) 28 { 29 int lenth=0; 30 while(*(str+lenth) != '\0') { 31 lenth++; 32 } 33 return lenth; 34 } 35 /* 36 函数功能:两个字符串的比较 37 */ 38 int str_cmp(char *str1, char *str2) 39 { 40 int i=0,a=0; 41 for(i=0; str1[i] == str2[i] && str1[i] != '\0';i++ ) { 42 ; 43 } 44 if(str1[i] != str2[i] ){ 45 a = str2[i] -str1[i]; 46 } 47 return a; 48 49 } 50 /* 51 函数功能:字符串的复制 52 */ 53 void strcopy(char *str1, char *str2) 54 { 55 int i=0; 56 while(str2[i] != '\0') { 57 str1[i] = str2[i]; 58 i++; 59 } 60 str1[i] = '\0'; 61 62 } 63 /* 64 函数功能: 字符串的连接 65 */ 66 void str_cat(char *str1, char *str2,char *str3) 67 { 68 int i,j; 69 for(i=0;str1[i] != '\0';i++) { 70 str3[i] = str1[i]; 71 } 72 for(j=0;str2[j] != '\0';j++) { 73 str3[i+j] = str2[j]; 74 } 75 } 76 61,0-1 65%