在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、字符串操作主要包括字符串复制、字符串比较和字符串拼接 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main(int argc, char **argv) 6 { 7 char *ptrArr1 = (char *)malloc(sizeof(char) * 10); 8 strcpy(ptrArr1, "guochaoteacher"); 9 printf("ptrArr1: %s\n", ptrArr1); 10 11 char *ptrArr2 = (char *)malloc(strlen("guochaoteacher") + 1); 12 strcpy(ptrArr2, "guochaoteacher"); 13 printf("ptrArr2: %s\n", ptrArr2); 14 15 return 0; 16 }
4、字符串比较strcmp: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main(int argc, char **argv) 6 { 7 char *command = (char *)malloc(sizeof(char) * 10); 8 printf("please input command: "); 9 scanf("%s", command); 10 printf("command: %s\n", command); 11 if(strcmp(command, "Quit") == 0 || strcmp(command, "quit") == 0){ 12 printf("You input the command: %s", command); 13 }else{ 14 printf("You can't quit!\n"); 15 } 16 17 return 0; 18 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main(int argc, char **argv) 6 { 7 char *ptrArr1 = (char *)malloc(sizeof(char) * 15); 8 ptrArr1 = "Hello"; 9 printf("ptrArr1: %s and %p\n", ptrArr1, ptrArr1); 10 char *ptrArr2 = (char *)malloc(sizeof(char) * 25); 11 strcat(ptrArr2, ptrArr1); 12 printf("ptrArr2: %s and %p\n", ptrArr2, ptrArr2); 13 strcat(ptrArr2, " World!"); 14 printf("ptrArr2: %s and %p\n", ptrArr2, ptrArr2); 15 16 return 0; 17 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论