(ps:提前声明一下:课后习题是备份给自己看的😂)
1.输入3个整数,按由小到大的顺序输出
int main() {
void swap(int *p1, int * p2);
int n1, n2, n3;
int *p1, *p2, *p3;
printf("input three integer n1,n2,n3:");
scanf("%d,%d,%d",&n1,&n2,&n3);
p1 = &n1;
p2 = &n2;
p3 = &n3;
if (n1 > n2)
swap(p1, p2);
if (n1 > n3)
swap(p1, p3);
if (n2 > n3)
swap(p2, p3);
printf("Now,the order is:%d,%d,%d\n",n1,n2,n3);
return 0;
}
void swap(int *p1, int *p2) {
int p;
p = *p1;
*p1 = *p2;
*p2 = p;
}
远行结果:2.输入三个字符串,按由小到大的顺序输出
//记得导入头文件:#import <string.h>
int main() {
void swap(char *, char *);
char str1[20],str2[31],str3[20];
printf("input three line:\n");
gets(str1);
gets(str2);
gets(str3);
if (strcmp(str1, str2) > 0)
swap(str1, str2);
if (strcmp(str1, str3) > 0)
swap(str1, str3);
if (strcmp(str2, str3) > 0)
swap(str2, str3);
printf("Now,the order is:\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
return 0;
}
void swap(char *p1, char *p2){
char p[20];
strcpy(p, p1);
strcpy(p1, p2);
strcpy(p2, p);
}
运行结果:(注:在Xcode中运行才显示的warning: this program uses gets(), which is unsafe.关于使用gets()不安全的警告)
3.写一函数,求一个字符串的长度。在main函数中输入字符串,并输入其长度
int main() {
int length(char *p);
int len;
char str[20];
printf("input string: ");
scanf("%s",str);
len = length(str);
printf("The length of string is %d. \n",len);
return 0;
}
int length(char *p) { //求字符串长度函数
int n = 0;
while (*p != '\0') {
n++;
p++;
}
return(n);
}
运行结果:4.有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串
//记得导入头文件:#import <string.h>
int main() {
void copystr(char *, char *, int);
int m;
char str1[20],str2[20];
printf("input string:\n");
gets(str1);
printf("which character that begin to copy?");
scanf("%d",&m);
if (strlen(str1) < m)
printf("input error!");
else {
copystr(str1, str2, m);
printf("result:%s\n",str2);
}
return 0;
}
void copystr(char *p1, char *p2, int m) { //字符串部分复制函数
int n = 0;
while (n < m - 1) {
n++;
p1++;
}
while (*p1 != '\0') {
*p2 = *p1;
p1++;
p2++;
}
*p2 = '\0';
}
运行结果:(注:在Xcode中运行才显示的warning: this program uses gets(), which is unsafe.关于使用gets()不安全的警告)
5.输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少
//记得导入头文件:#import <string.h>
int main() {
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
char *p,s[20];
printf("input string: ");
while ((s[i] = getchar()) != '\n')
i++;
p = &s[0];
while (*p != '\n') {
if (('A' <= *p) && (*p <= 'Z'))
++upper;
else if (('a' <= *p) && (*p <= 'z'))
++lower;
else if (*p == ' ')
++space;
else if ((*p <= '9') && (*p >= '0'))
++digit;
else
++other;
p++;
}
printf("upper case:%d lower case:%d",upper,lower);
printf(" space:%d digit:%d other:%d\n",space,digit,other);
return 0;
}
运行结果:6.将n个数按输入时顺序的逆序排列,用函数实现
int main() {
void sort(char *p, int m);
int i, n;
char *p, num[20];
printf("input n:");
scanf("%d",&n);
printf("please input these numbers:\n");
for (i = 0; i < n; i++)
scanf("%d",&num[i]);
p = &num[0];
sort(p, n);
printf("Now,the sequence is:\n");
for (i = 0; i < n; i++)
printf("%d",num[i]);
printf("\n");
return 0;
}
void sort(char *p, int m) { //将n个逆序排列函数
int i;
char temp, *p1, *p2;
for (i = 0; i < m/2; i++) {
p1 = p + i;
p2 = p + (m - 1 - i);
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
}
运行结果: