赞
踩
(创作不易,感谢有你,你的支持,就是我前行的最大动力,如果看完对你有帮助,请留下您的足迹)
本关任务:程序功能是通过调用
reverse()
函数按逆序重新放置数组a
中的元素值,请补全程序。测试输入:
0 1 2 3 4 5 6 7 8 9
预期输出:
9 8 7 6 5 4 3 2 1 0
数组倒置只需要将相应的指针交换位置即可,原题给出大部分代码,我们只需要补充细节即可
- #include "stdio.h"
- #define N 10
- void reverse(int* p, int a, int b)
- {
- int c;
-
- /***** 请在以下一行填写代码 *****/
- while (a < b)
-
- {
- c = *(p + a);
-
- /***** 请在以下一行填写代码 *****/
- *(p + a) = *(p + b);
-
- *(p + b) = c;
-
- a++;
- /***** 请在以下一行填写代码 *****/
- b--;
-
- }
- }
- int main()
- {
- int a[N], i;
- for (i = 0; i < N; i++)
-
- /***** 请在以下一行填写代码 *****/
- scanf("%d", &a[i]);
-
- reverse(a, 0, N - 1);
-
- for (i = 0; i < N; i++)
-
- /***** 请在以下一行填写代码 *****/
- printf("%d ", a[i]);
-
- printf("\n");
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
本关任务:对某一个长度为
7
个字符的字符串, 除首、尾字符之外,要求对中间的5
个字符按ASCII
码降序排列。例如,原来的字符串为
CEAedca
,排序处理后应输出为CedcEAa
。
这题和上一题很相似,只不过从原本的全部逆序变成了指定位置降序,只需要用for循环找到指针中对应的位置,然后排序即可
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- void fun(char* s)
- {
- char ch;
- int i, j;
- for (i = 1; i < 6; i++)
- {
- for (j = i + 1; j < 6; j++)
- {
-
- /***** 请在以下一行填写代码 *****/
- if (*(s + j) > *(s + i))
-
- {
- ch = *(s + j);
- *(s + j) = *(s + i);
- *(s + i) = ch;
- }
- }
- }
- }
- int main()
- {
- char s[10];
-
- scanf("%s", s);
-
- /***** 请在以下一行填写代码 *****/
- fun(s);
-
- printf("%s", s);
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
本关任务:给定程序中函数
fun
的功能是从N
个字符串中找出最长的那个串,并将其地址作为函数值返回。N
个字符串在主函数中输入,并放入一个字符串数组中。请改正程序中的错误,使它能得出正确结果。注意:不要改动main
函数,不得增行或删行,也不得更改程序的结构。测试输入:
a
bb
ccc
dddd
eeeee
预期输出:
The 5 string :
a
bb
ccc
dddd
eeeee
The longest string :
eeeee
本题要改两个错误点,第一个就是fun函数的类型应该与longest相同,都为char*;第二个就是在返回值时应该返回的是sp而不是sq
- #include <stdio.h>
- #include <string.h>
- #define N 5
- #define M 81
-
- /***** 以下一行有错误 *****/
- char* fun(char(*sq)[M])
- {
- int i;
- char *sp;
- sp=sq[0];
- for(i=0;i<N;i++)
- if(strlen(sp)<strlen(sq[i]))
- sp=sq[i];
-
- /***** 以下一行有错误 *****/
- return sp;
-
- }
- int main()
- {
- char str[N][M], *longest;
- int i;
-
-
- for(i=0; i<N; i++)
- scanf("%s",str[i]);
-
- printf("The %d string :\n",N);
-
- for(i=0; i<N; i++)
- puts(str[i]);
- longest = fun(str);
-
- printf("The longest string :\n");
- puts(longest);
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
本关任务:规定输入的字符串中只包含字母和
*
号。给定程序的功能是将字符串中的前导*
号全部移到字符串的尾部。请将程序补充完整,使其能正确运行得出结果。测试输入:
***abcd
预期输出:
abcd***
本题只需要填写3次代码,第一次:while循环统计*的个数,所以是p++;第二次:由第三个while循环可以看出其打印的是*,所以第二个while循环打印的就是*后面的数;第三次:因为开始打印*,所以每打印一次,n 就要 --
- #include <stdio.h>
- void fun( char *a )
- {
- int i=0,n=0;
- char *p;
- p=a;
-
- while (*p=='*')
- {
- n++;
-
- /***** 请在以下一行填写代码 *****/
- p++ ;
-
- }
- while(*p)
- {
-
- /***** 请在以下一行填写代码 *****/
- a[i]=*p ;
-
- i++;
- p++;
- }
- while(n!=0)
- {
- a[i]='*';
- i++;
- /***** 请在以下一行填写代码 *****/
- n-- ;
- }
- a[i]='\0';
- }
- int main()
- {
- char s[81];
- int n=0;
-
- scanf("%s",s);
-
- fun( s );
-
- printf("The string after oveing: \n");
- puts(s);
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。