1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.h> 3 #include <string.h> 4 #include <assert.h> 5 #include <stdlib.h> 6 #include <ctype.h> 7 //模拟实现 - strlen 8 int my_strlen(const char* str) 9 { 10 int count = 0; 11 assert(str != NULL);//断言,不能等于空指针 12 while (*str != '\0')//==>while(*str) 13 { 14 count++; 15 str++; 16 } 17 return count; 18 } 19 int main() 20 { 21 //int len = strlen("abcdef"); 22 char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; 23 int len = strlen(arr); 24 printf("%d\n", len); 25 system("pause"); 26 return 0; 27 } 28 29 //写一个自己的strlen函数 30 //1.计数器 31 //2.递归 - 不创建临时变量 32 //3.指针-指针 33 34 int main() 35 { 36 if (strlen("abc") - strlen("abcdef") > 0) 37 { 38 printf("hehe\n"); 39 } 40 else{ 41 printf("haha\n"); 42 } 43 system("pause"); 44 return 0; 45 } 46 47 //模拟实现 - strcpy 48 char* my_strcpy(char* dest, const char*src) 49 { 50 //保证指针有效性 51 assert(dest != NULL); 52 assert(src != NULL); 53 //返回的是目的地的起始位置 54 char* ret = dest; 55 //拷贝src指向的字符串到dest指向的空间,包含'\0' 56 while (*dest++ = *src++) 57 { 58 ; 59 //dest++; 60 //src++; 61 } 62 //返回目的空间的起始位置 63 return ret; 64 //拷贝的是 "\0" 65 //*dest = *src; 66 } 67 int main() 68 { 69 char arr1[] = "abcdefghi"; 70 char arr2[] = "bit"; 71 my_strcpy(arr1, arr2); 72 printf("%s\n", arr1); 73 system("pause"); 74 return 0; 75 } 76 77 模拟实现 - strcat 78 char* my_strcat(char* dest, const char*src) 79 { 80 char* ret = dest; 81 assert(dest!=NULL);//断言 82 assert(src);//断言 83 //1.找到目的字符串的'\0' 84 while (*dest != '\0') 85 { 86 dest++; 87 } 88 //2.追加 89 while (*dest++ = *src++) 90 { 91 ; 92 } 93 return ret; 94 } 95 int main() 96 { 97 char arr1[30] = "hello"; 98 char arr2[] = "world"; 99 my_strcat(arr1, arr2);//将arr2的内容追加到arr1中去 100 printf("%s\n", arr1); 101 system("pause"); 102 return 0; 103 } 104 105 106 //模拟实现 - strcmp 107 int main() 108 { 109 char* p1 = "abcdef"; 110 char* p2 = "sqwer"; 111 //int ret = (strcmp(p1, p2); 112 if (strcmp(p1, p2) == 1) 113 { 114 printf("p1>p2\n"); 115 } 116 else if (strcmp(p1, p2) == 0) 117 { 118 printf("p1=p2\n"); 119 } 120 else { 121 printf("p1<p2\n"); 122 } 123 printf("%d\n", ret); 124 system("pause"); 125 return 0; 126 } 127 128 int my_strcmp(const char* str1, const char* str2) 129 { 130 assert(str1 && str2);//断言 131 //比较 132 while (*str1 == *str2) 133 { 134 if (*str1 == '\0') 135 { 136 return 0;//相等 137 } 138 str1++; 139 str2++; 140 } 141 if (*str1 > *str2) 142 { 143 return 1;//大于 144 } 145 else { 146 return -1;//小于 147 } 148 //return (*str1-*str2); 149 } 150 int main() 151 { 152 char* p1 = "abcdef"; 153 char* p2 = "abqwe"; 154 int ret = my_strcmp(p1, p2); 155 printf("%d\n", ret); 156 system("pause"); 157 return 0; 158 } 159 160 int main() 161 { 162 char arr1[5] = "abc"; 163 char arr2[] = "hello bit"; 164 int ret = strncpy(arr1, arr2, 4); 165 printf("%s\n", ret); 166 } 167 168 int main() 169 { 170 //strncmp - 字符串比较 171 const char* p1 = "abcdef"; 172 const char* p2 = "abcqwr"; 173 //int ret = strcmp(p1, p2); 174 int ret = strncmp(p1, p2, 3); 175 printf("%d\n", ret); 176 system("pause"); 177 return 0; 178 } 179 180 181 //模式实现 - strstr 182 char* my_strstr(const char* p1,const char* p2) 183 { 184 assert(p1 != NULL);//断言,不能等于空指针 185 assert(p2 != NULL);//断言,不能等于空指针 186 char *s1 = p1; 187 char *s2 = p2; 188 char *cur = (char*)p1; 189 if (*p2 == '\0') 190 { 191 return (char*)p1; 192 } 193 while (*p1) 194 { 195 s1 = cur; 196 s2 = (char*)cur; 197 while ((*s1 != '\0') && (*s2 != '\0') && (*s1 == *s2)) 198 { 199 s1++; 200 s2++; 201 } 202 if (*s2 == '\0') 203 { 204 return cur;//找到子串 205 } 206 cur++; 207 } 208 return NULL;//找不到子串 209 } 210 int main() 211 { 212 char *p1 = "abcdef"; 213 char *p2 = "def"; 214 char* ret = my_strstr(p1, p2); 215 if (ret == NULL) 216 { 217 printf("子串不存在\n"); 218 } 219 else{ 220 printf("%s\n", ret); 221 } 222 223 system("pause"); 224 return 0; 225 } 226 227 int main() 228 { 229 //192.168.32.121 230 //char arr[] = "dingchenxi@wspc.student"; 231 char arr[] = "192.168.32.121"; 232 char*p = ".";//要分割中字符串中的分割符号 233 234 char buf[1024] = { 0 }; 235 strcpy(buf, arr); 236 //切割buf中的字符串 237 char* ret = NULL; 238 for (ret = strtok(arr, p); ret != NULL; ret=strtok(NULL,p)) 239 { 240 printf("%s\n", ret); 241 } 242 243 system("pause"); 244 return 0; 245 } 246 247 int main() 248 { 249 char* str = strerror(1); 250 /*0 - No error 251 1 - Operation not permitted 252 2 - No such file or directory*/ 253 //errno - 是一个全局的错误码的变量 254 //当C语言的库函数在执行过程中发生了错误,就会把对应的错误码赋值到errno中 255 printf("%s\n", str); 256 system("pause"); 257 return 0; 258 } 259 260 int main() 261 { 262 FILE * pf = fopen("test.txt", "r"); 263 if (pf == NULL) 264 { 265 printf("%s\n", strerror(errno)); 266 } 267 else{ 268 printf("open file success\n"); 269 } 270 system("pause"); 271 return 0; 272 } 273 274 int main() 275 { 276 char ch = 'w'; 277 int ret = islower(ch); 278 printf("%d\n", ret); 279 system("pause"); 280 return 0; 281 } 282 283 int main() 284 { 285 char ch = tolower('Q');//小写不动,大写转小写 286 puchar(ch); 287 system("pause"); 288 return 0; 289 } 290 291 int main() 292 { 293 char ch = toupper('q');//小写转大写,大小写都可以 294 putchar(ch); 295 system("pause"); 296 return 0; 297 } 298 299 300 int main() 301 { 302 char arr[] = "I Am A Student"; 303 int i = 0; 304 while (arr[i]) 305 { 306 if (isupper(arr[i])) 307 { 308 arr[i] = tolower(arr[i]); 309 } 310 i++; 311 } 312 printf("%s\n", arr); 313 system("pause"); 314 return 0; 315 }