C:函数:功能:实现字符数组中所有字母的倒序存放并输出
前两天小测碰到一道题,建立一个函数,功能:实现字符数组中所有字母的倒序存放并输出,一开始觉得简单跟数字数组差不多,运行一下发现很多格式错误,这些是不必要的错误,现在就来说下,先说一下代码思路:定义一个字符数组假如有n个字符,让一个指针变量*p指向首地址,*(p+n-1)就是末地址,然后实现*p与*(p+n-1)值的互换,这边让*q=*(p+n-1)每换一次,p++,q--,当p>q时退出循环,我们来看下代码:
#include "stdio.h"
char change(char *p,int y);
void main()
{
char a[]="abcdef";
change(a,);
printf("%s",a);
}
char change(char *p,int y)
{
char *q;
int temp;//这边比较容易错,有的定性思维,会定义成char类型;
q=p+y-;
while(p<q)
{
temp=*p;
*p=*q;
*q=temp;
p++;
q--;
}
}
这边只是令n=6,看的更清晰一点,要复杂一点的话,我们也可以自己输入字母,想要几个就几个,这次我用gets()来输入,假如输入n个,那我们怎么知道这个n的值,我们可以先定义*p1让p=a,既指向首地址,要是超过末地址也就是*(p+n)==0,这就给我们一个结束循环的条件:
#include "stdio.h"
void main()
{
char a[];
gets(a);
char *p;
int i,n;
i=;
p=a;
while(*(p+i)!=)
{
i++; }
n=i;
printf("%d",n);
}
上面代码可以求出我们手动输入字母的个数再加上change()函数就可以了:
#include "stdio.h"
char change(char *p,int y);
void main()
{
char a[];
printf("input letter:\n");
gets(a);
char *p1;
int i,n;
i=;
p1=a;
while(*(p1+i)!=)//超出地址结果为0
{
i++;
}
n=i;
change(a,n);
printf("output:\n%s",a);
}
char change(char *p,int y)
{
char *q;
int temp;
q=p+y-;
while(p<q)
{
temp=*p;
*p=*q;
*q=temp;
p++;
q--;
}
}
运行结果:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoYAAAGtCAIAAAAEayJGAAAVZUlEQVR4nO3dXW8c130H4Lkp2o+wH4AX/iQFeGeYEeoiQK960wAsUKAtwLiGUS+qtG6QtELaNE6ZJvFLZFqSZcmSqLfVC02LsmhLdpxu6wTVVVGkQD/D9oLk7rzv7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RDAAhTCI5SRKRDABdSZ1LdsU1AHRncsX1rZ2nVx/siGQA6ESvPzx/7c4Hd7eSWztPrz4UyQDQjV5/+N71vUh+/OyjrcciGQA60esPNzYHlwfbyZ0nX17/+IlIBoBO9PrD92/e+/DeJ8nd3V/e+OQzkQwAnej1hxdvP7j6YCcZfPbVzZ2nIhkAOtHrDy8PPr629Wny4NnwzpMve/2hyWQymUymTqarD3ZufPJZ8vDL/xx8/qvbj59d3969+nDnw3ufXLqzdfHWgws3729sDt67fvf8tdvvXr35zpXNtz/cfPvDGyaT6bhMly5d2traSv/d3Nzc3Nx85ZVXXnzxxb2//b/5uz87dyPm1HkDmkxtT5vvXNl89+rN89duv3f97sbm4MKt+x/c3bpy/9H17d1bj58l21/95uEX/zH47Fe3P/3i5qPPb2zvXtv69OqDnSv3P7k82P7g7talOw8v3Lp/4eb99zfvbWwOTCbTcZmuXbv25MmT9N979+7du3fvtdde+8Y3vrH392/f+P5f/nAQc+q8AU2mFqYbB9Pm4P3Nexdu3r9w6/6lOw8/uLt1ebB95cGjjx4+vrG9e2vn6d3dX/4/Futry+NKTQkAAAAASUVORK5CYII=" alt="" />,要是数字数组的话也是差不多的。
C:函数:功能:实现字符数组中所有字母的倒序存放并输出的更多相关文章
- python实现给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果!
题目描述:给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果! 样例: input:[["a","b"," ...
- 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果
package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...
- C语言:将形参s所指字符串中所有ASCII码值小于97的字符存入形参t所指字符数组中,
//将形参s所指字符串中所有ASCII码值小于97的字符存入形参t所指字符数组中,形成一个新串,并统计出符合条件的字符个数返回. //关注点:使用*(t+n)的方式可以不改变指针的指向,像数组一样处理 ...
- Numpy.frompyfunc()将计算单个值的函数转化为计算数组中每个元素的函数
Numpy.frompyfunc()将计算单个值的函数转化为计算数组中每个元素的函数 不再通过遍历,对数组中的元素进行运算,利用frompyfunc()将计算单个值的函数转化为计算数组中每个元素的函数 ...
- 笔试题&面试题:找出一个数组中第m小的值并输出
题目:找出一个数组中第m小的值并输出. 代码: #include <stdio.h> int findm_min(int a[], int n, int m) //n代表数组长度,m代表找 ...
- PTA 将数组中的数逆序存放
7-1 将数组中的数逆序存放 (20 分) 本题要求编写程序,将给定的n个整数存入数组中,将数组中的这n个数逆序存放,再按顺序输出数组中的元素. 输入格式: 输入在第一行中给出一个正整数n(1). ...
- C语言:返回两个数组中第一个元素的指针,并输出这个值
// // main.c // Pointer_search // // Created by ma c on 15/8/2. // Copyright (c) 2015年. All righ ...
- C从控制台(stdin)输入带空格的字符串到字符数组中
用scanf("%s",array); 的话遇到空格就停止接收后面的字符了,那怎么才能接收带空格的字符串呢? 1.用 gets() ,它可以接收带空格的字符串, 直到回车才结束输入 ...
- golang中数组指针和指针数组当做函数参数如何修改数组中的值
先理解:数组指针它的类型时指针,指针数组它的类型时数组 1. 数组指针当做函数的参数 package main import "fmt" func changeData(dataA ...
随机推荐
- 如何用webgl(three.js)搭建一个3D库房-第一课
今天我们来讨论一下如何使用当前流行的WebGL技术搭建一个库房并且实现实时有效交互 第一步.搭建一个3D库房首先你得知道库房长啥样,我们先来瞅瞅库房长啥样(这是我在网上找的一个库房图片,百度了“库房” ...
- [OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
在上一篇基于OIDC的SSO的中涉及到了4个Web站点: oidc-server.dev:利用oidc实现的统一认证和授权中心,SSO站点. oidc-client-hybrid.dev:oidc的一 ...
- Bootstrap里的文件分别表示什么?都有什么用?
bootstrap.css 是完整的bootstrap样式表,未经压缩过的,可供开发的时候进行调试用bootstrap.min.css 是经过压缩后的bootstrap样式表,内容和bootstrap ...
- 使用sklearn进行数据挖掘-房价预测(3)—绘制数据的分布
使用sklearn进行数据挖掘系列文章: 1.使用sklearn进行数据挖掘-房价预测(1) 2.使用sklearn进行数据挖掘-房价预测(2)-划分测试集 3.使用sklearn进行数据挖掘-房价预 ...
- GDI+ 应用,Release没有错误,Debug很多。
问题描述: 在VC6环境中,一个界面工程利用了GDI+的一些库,Release模式下编译.运行无误,但在Debug模式下就会报告错误. xxx.cpp D:\Program Files\Microso ...
- 34.Linux-printk分析、使用prink调试驱动
本节学习目的 1)分析printk()函数 2)使用prink()调试驱动 1.在驱动调试中,使用printk(),是最简单,最方便的办法 当uboot的命令行里的“console=tty1”时,表示 ...
- angular指令监听ng-repeat渲染完成后执行自定义事件方法
今天工作中遇到需要用到ng-repeat遍历渲染完后执行某个操作,angular本身并没有提供监听ng-repeat渲染完成的指令,所以需要自己创建自定义指令. 在ng-repeat模板实例内部会暴露 ...
- dij洛谷电车
//Gang #include<iostream> #include<cstring> #include<algorithm> #include<cstdio ...
- redis函数总结
<?php /*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host, ...
- gulp入门详细教程
简介:gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以很愉快的编写代码, ...