一、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 ", 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 ", 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 ", 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]!='