一、for /l %i in (1,1,5) do calc 等命令行参数
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 void main() { 6 /*char str[] ="for /l %i in (1,1,5) do calc"; 7 char *p = "for /l %i in (1,1,5) do calc";*/ 8 //str[0] = ‘ ‘; str是数组,存储的是字符串的每一个字符 9 //*p = ‘ ‘; p是指针,存储字符串的地址,所以不能对其赋值 10 11 char str[100] = { 0 }; 12 int num; 13 char op[30] = { 0 }; 14 scanf("%d %s", &num, op); 15 printf("for /l %%i in (1,1,%d) do %s",num,op); 16 sprintf(str,"for /l %%i in (1,1,%d) do %s", num, op); 17 system(str); 18 19 system("pause"); 20 }
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h>o 4 5 void main() { 6 char str[100] = { 0 }; 7 char op[30] = { 0 }; 8 scanf("%s", op); 9 sprintf(str,"taskkill /f /im %s",op); 10 system(str); 11 12 system("pause"); 13 }
二、变色龙
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<Windows.h> 5 6 void main() { 7 system("tasklist"); 8 while (1) 9 { 10 for (int i = 0x0; i <= 0xf; i++)//0x 16进制 11 { 12 char str[30] = { 0 };//存储指令 13 sprintf(str, "color %x%x", i, 0xf - i);//打印指令 14 system(str);//变色 15 Sleep(200); 16 } 17 } 18 system("pause"); 19 }
三、gets和puts(对比scanf和printf)
puts()函数自动换行。
四、strstr(在串中查找指定字符串的第一次出现)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 void main() { 6 char str1[50] = "my name is yincheng"; 7 char str2[20] = "chen"; 8 char *p = strstr(str1, str2); 9 10 if (p == NULL) printf("没找到!"); 11 else 12 { 13 printf("找到了,%p,%c\n", p,*p);//%p:地址类型 14 } 15 system("pause"); 16 }
五、strcmp以及自己实现这个函数功能(mystrcmp)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string> 4 5 void main() { 6 char str1[50] = "hello yincheng"; 7 char str2[50] = "hello yincheng"; 8 int num; 9 num = strcmp(str1, str2); 10 printf("%d\n", num);//num==0表明相等 11 if (num == 0) //验证密码 12 printf("字符串相等"); 13 else 14 printf("字符串不等"); 15 16 system("pause"); 17 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string> 4 5 void main() { 6 //Windows排序忽略大小写,strcmp严格区分大小写 7 char str1[50] = "BaAAA"; 8 char str2[50] = "BAAAA"; 9 //字符都有编号,大写A65,小写a97; 10 //字符串比较:从左到右依次比较 11 12 int num; 13 num = strcmp(str1, str2);//对比大小 14 printf("%d\n", num);//num==0表明相等 15 if (num < 0) 16 printf("第一个字符串比较小"); 17 else if(num>0) 18 printf("第一个字符串比较大"); 19 else 20 printf("两个字符串相等"); 21 system("pause"); 22 }
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string> 5 6 int mystrcmp(char *p1, char *p2) { 7 int i = 0; 8 while (p1[i]==p2[i]&&p1[i]!=‘\0‘) 9 { 10 i++; 11 } 12 int num;//代表返回值 13 if (p1[i] == ‘\0‘ && p2[i] == ‘\0‘) 14 num = 0;//判断相等 15 else 16 num = p1[i] - p2[i]; 17 return num; 18 } 19 20 void main() { 21 char str1[50] = "AppCompat"; 22 char str2[50] = "AppPatch"; 23 _strupr(str1);//全部升级为大写 24 _strupr(str2); 25 printf("%s\n%s\n",str1,str2);//打印字符串 26 int num = mystrcmp(str1, str2); 27 if (num < 0) 28 printf("第一个字符串比较小"); 29 else if(num>0) 30 printf("第一个字符串比较大"); 31 else 32 printf("两个字符串相等"); 33 system("pause"); 34 }
六、strncmp及strchr
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 void main() { 6 char str1[30] = "notepad1"; 7 char str2[30] = "notepadcalc"; 8 if (strncmp(str1, str2, 7) == 0) 9 printf("相等"); 10 else 11 printf("不相等"); 12 13 system("pause"); 14 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 void main() { 6 char str[30] = "notepad1"; 7 //char *p = strchr(str, ‘t‘);//找到 8 char *p = strchr(str+3, ‘t‘);//没找到 9 //strchr第一个参数可以从任意位置检索字符 10 if (p==NULL) 11 printf("没找到\n"); 12 else 13 printf("找到%p,%c\n",p,*p); 14 15 system("pause"); 16 }
七、字符串二级指针
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 char str1[30] = "notepad"; 5 char str2[30] = "tasklist"; 6 7 //函数有副本机制,形式参数会开辟内存,新建一个变量,容纳传递过来的实际参数 8 void change(char * str) { 9 printf("change-%p\n", &str); 10 str = str1;//改变指针指向 11 } 12 13 //改变一个变量,需要变量的地址;如果变量是地址,需要二级指针 14 void changep(char **pp) { 15 *pp = str1; 16 } 17 18 void main() { 19 char *p = str2; 20 //change(p); 21 changep(&p); 22 printf("main-%p\n", &p); 23 system(p); 24 25 system("pause"); 26 }
时间: 2024-10-12 21:28:23