输入一个正整数n(1<n≤10),再输入一维字符数组a中的n个元素,将数组中的值按逆序存放并输出。要求自定义并调用一个函数void fun(char *a,int n)实现对a指向的数组中n个元素逆序
时间: 2023-04-11 10:03:34 浏览: 74
void fun(char *a, int n){
char temp;
for(int i=; i<n/2; i++){ //循环次数为n/2
temp = *(a+i); //将a[i]的值暂存到temp中
*(a+i) = *(a+n-i-1); //将a[i]的值替换为a[n-i-1]的值
*(a+n-i-1) = temp; //将a[n-i-1]的值替换为temp中的值
}
}
//主函数中调用fun函数
int main(){
int n;
char a[10];
cin>>n;
for(int i=; i<n; i++){
cin>>a[i];
}
fun(a,n); //调用函数
for(int i=; i<n; i++){
cout<<a[i]<<" ";
}
return ;
}
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)