1.循环输入5个人姓名(二维字符数组),计算最大值
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 int main(int argc, const char *argv[])
5 {
6 char a[5][100];
7 int i;
8 for(i=0;i<5;i++)
9 scanf("%s",a[i]);
10 char max[100];
11 strcpy(max,a[0]);
12 for(i=0;i<5;i++)
13 {
14 if(strcmp(max,a[i])<0)
15 strcpy(max,a[i]);
16 }
17 printf("最大值为%s\n",max);
18 return 0;
19 }
2.在主函数中定义字符串数组并初始化,自定有参有返函数,计算字符串长度,并返回长度
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 unsigned long length(char str[5])
5 {
6 int i=0;
7 for(i=0;str[i]!='0';i++);
8 return i;
9 }
10 int main(int argc, const char *argv[])
11 {
12 char str[5]="abcd";
13 unsigned long len=length(str);
14 printf("%ld\n",len);
15 return 0;
16 }
3.在主函数中定义两个字符串并初始化,定义有参无返函数,实现字符串连接
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 void fun(char a[20],char b[20])
5 {
6 int i,j;
7 for(i=0;a[i]!='\0';i++);
8 for(j=0;b[j]!='\0';j++)
9 a[i++]=b[j];
10 a[i]='\0';
11 puts(a);
12 }
13 int main(int argc, const char *argv[])
14 {
15 char a[20]="hello";
16 char b[20]="world";
17 fun(a,b);
18 return 0;
19 }
4.在主函数中定义两个字符串并初始化,定义有参有返函数,实现字符串比较,返回两个字符串ASCII值的差
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 int ASCII(char s1[],char s2[])
5 {
6 int i=0;
7 while(s1[i]==s2[i])
8 {
9 if(s1[i]=='\0')
10 break;
11 i++;
12 }
13 int sub=s1[i]-s2[i];
14 if(sub>0) puts("s1>s2");
15 else if(sub<0) puts("s1<s2");
16 else puts("s1==s2");
17 return sub;
18 }
19 int main(int argc, const char *argv[])
20 {
21 char s1[]="ABCD";
22 char s2[]="ABCd";
23 ASCII(s1,s2);
24 printf("%d\n",ASCII(s1,s2));
25 return 0;
26 }
思维导图: