通过指针及函数调用,将数组a中的7个字母{s,c,z,a,m,n,k}
时间: 2023-12-17 14:05:43 浏览: 15
按字典序从小到大排序后输出。
以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
void swap(char *a, char *b) {
char temp = *a;
*a = *b;
*b = temp;
}
void sort(char *a, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
swap(&a[j], &a[j + 1]);
}
}
}
}
int main() {
char a[] = {'s', 'c', 'z', 'a', 'm', 'n', 'k'};
int n = sizeof(a) / sizeof(a[0]);
sort(a, n);
for (int i = 0; i < n; i++) {
printf("%c ", a[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
a c k m n s z
```
相关问题
再去c语言中通过指针及函数调用,将数组a中的7个字母{s,c,z,a,m,n,k}按相反顺序存放
可以使用指针和函数来实现将数组中的元素按相反顺序存放。具体实现步骤如下:
1. 定义一个指向字符类型的指针变量p,让它指向数组a的第一个元素。
2. 定义一个整型变量i,并初始化为0。
3. 定义一个函数reverse,函数的参数是指向字符类型的指针变量p和整型变量len,表示数组的长度。
4. 在函数内部,使用循环将数组中的元素按相反顺序存放。具体实现步骤如下:
- 定义一个指向字符类型的指针变量q,让它指向数组a的最后一个元素。
- 使用循环遍历数组a,将a[i]和q所指向的元素交换位置。
- 每次循环,i自增1,q向前移动一位。
- 当i>=len/2时,停止循环。
5. 在主函数中,调用reverse函数,将数组a中的元素按相反顺序存放。
下面是完整的代码实现:
```c
#include <stdio.h>
void reverse(char *p, int len) {
char *q = p + len - 1; // 指向数组最后一个元素
while (p < q) {
char temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
}
int main() {
char a[] = {'s', 'c', 'z', 'a', 'm', 'n', 'k'};
int len = sizeof(a) / sizeof(char);
printf("Original array: ");
for (int i = 0; i < len; i++) {
printf("%c ", a[i]);
}
printf("\n");
reverse(a, len);
printf("Reversed array: ");
for (int i = 0; i < len; i++) {
printf("%c ", a[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
Original array: s c z a m n k
Reversed array: k n m a z c s
```
编写c语言程序通过指针及函数调用,将数组 a 中的 7 个字母{s,c,z,a,m,n,k} 按相反顺序存放。
以下是一个示例程序:
```c
#include <stdio.h>
void reverse_array(char *arr, int size) {
char temp;
for (int i = 0; i < size / 2; i++) {
temp = *(arr + i);
*(arr + i) = *(arr + size - i - 1);
*(arr + size - i - 1) = temp;
}
}
int main() {
char a[] = {'s', 'c', 'z', 'a', 'm', 'n', 'k'};
int size = sizeof(a) / sizeof(char);
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%c ", a[i]);
}
reverse_array(a, size);
printf("\nReversed array: ");
for (int i = 0; i < size; i++) {
printf("%c ", a[i]);
}
return 0;
}
```
运行结果:
```
Original array: s c z a m n k
Reversed array: k n m a z c s
```
这个程序定义了一个 `reverse_array` 函数,它接受一个 char 类型的指针和数组大小作为参数。在函数内部,使用一个循环将数组中的元素按相反的顺序存储。在主函数中,程序定义了一个包含 7 个字符的数组,并调用 `reverse_array` 函数来反转它。最后,程序打印出原始数组和反转后的数组。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)