将字符串在指定的地方以及元素个数实行逆置
将字符串在指定的地方以及元素个数实行逆置
程序代码如下:
/* 2017年3月13日08:57:37 功能:在指定的地方以及想要逆置元素个数 */ #include"stdio.h" int *revese(int *, int, int); //指向函数的指针,在调用函数中数据改变,将会传给主函数 int main() { int a[100] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; int position; int amount; printf("请输入开始逆置位置和个数用空格隔开:"); scanf("%d %d", &position, &amount); int *b = reverse(a, position, amount); for (int x = 0; x < 10; x++) //遍历输出字符数组中所有的元素 { printf("%d ",b[x]); } } int *reverse(int *a, int position, int amount) { int i = position - 1; int j = amount+i -1; int t; int x = amount/2; for (; x > 0; i++, j--) { t = a[j]; a[j] = a[i]; a[i] = t; x--; } return a; } /* 总结 在VC++6.0中显示的结果: ———————————————————————— 请输入开始逆置位置和个数用空格隔开:4 6 2 4 6 18 16 14 12 10 8 20 ———————————————————————— */