1 #import <Foundation/Foundation.h> 2 3 /* 4 求字符串長度 5 */ 6 int stringLength(char arr[]); 7 8 9 /* 10 復制字符串 11 將arr1 復制到 arr 12 */ 13 void stringCopy(char arr[],char arr1[]); 14 15 16 17 /* 18 拼接字符串 19 將arr1 加到 arr 後 20 */ 21 void stringCatch(char arr[],char arr1[]); 22 23 24 /* 25 比較字符串 26 */ 27 int stringCompare(char arr[],char arr1[]);
1 #import "FunctionOfArray.h" 2 3 int stringLength(char arr[]){ 4 int length = 0; 5 while (arr[length] != '\0') { 6 length++; 7 } 8 return length; 9 } 10 11 12 void stringCopy(char arr[],char arr1[]){ 13 int i = 0; 14 while (arr1[i] != '\0') { 15 arr[i] = arr1[i]; 16 i++; 17 } 18 arr[i] = '\0'; 19 } 20 21 void stringCatch(char arr[],char arr1[]){ 22 int k = stringLength(arr); 23 int j = stringLength(arr1); 24 for (int i = 0; i < j; i++) { 25 arr[k+i] = arr1[i]; 26 } 27 arr[k + j] = '\0'; 28 } 29 30 int stringCompare(char arr[],char arr1[]){ 31 int k = stringLength(arr); 32 int j = stringLength(arr1); 33 34 for (int i = 0; i < k && i< j; i++) { 35 if(arr[i] != arr1[i]){ 36 return (int)arr[i] - (int)arr1[i]; 37 } 38 } 39 if(k == j){ 40 return 0; 41 }else if(k > j ){ 42 return (int)arr[j] - (int)arr1[j]; 43 }else{ 44 return (int)arr[k] - (int)arr1[k]; 45 } 46 }