前两天小测碰到一道题,建立一个函数,功能:实现字符数组中所有字母的倒序存放并输出,一开始觉得简单跟数字数组差不多,运行一下发现很多格式错误,这些是不必要的错误,现在就来说下,先说一下代码思路:定义一个字符数组假如有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/D9oLk7rzv7O6Q8yf5PBgQu7OzZ86c3Zkfz5nZ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sX1rZ2nVx/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:函数:功能:实现字符数组中所有字母的倒序存放并输出的更多相关文章
-
面试题-->;写一个函数,返回一个数组中所有元素被第一个元素除的结果
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()将计算单个值的函数转化为计算数组中每个元素的函数 ...
-
笔试题&;amp;面试题:找出一个数组中第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 ...
-
面试题:编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ";";。(c++实现)
实例说明 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ...
随机推荐
-
关于B树的一些总结
B树的定义 一棵m阶的B树满足下列条件: 树中每个结点至多有m个孩子. 除根结点和叶子结点外,其它每个结点至少有m/2个孩子. 根结点至少有2个孩子(如果B树只有一个结点除外). 所有叶结点在同一层, ...
-
iOS static
获得20条news 先实现,再提取到business 层. The static Keyword You can have a local variable retain its value thro ...
-
jquery 插件的编写
/** * 插件的学习 * 原文地址:http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html#home */ ;(function($, ...
-
学习HTML的第一次课
1.母板中的标签的含义 1.1< Doctype html/ > 声明文档类型为HTML 1.2< meta charset="UTF-8"/ > 设当前网 ...
-
latex中插入eps文件
\documentclass{article} \usepackage{graphicx}\usepackage{epstopdf} \begin{document}\begin{figure} \ ...
-
Java内存管理-掌握虚拟机类加载机制(四)
勿在流沙筑高台,出来混迟早要还的. 做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 上一篇介绍了整个JVM运行时的区域,以及简单对比了JDK7和JDK8中JVM运行时区域 ...
-
windows service卸载
.使用组合键win+r 调出服务页面 2.查看想要删除的服务的名称:如: 3.执行删除操作
-
loadrunner分析结果二
5 worst transaction transaction name:事务名 failure ratio[%](exceeded time/transaction duration)失败率 (超标 ...
-
mui---自定义页面打开的方向
在使用MUI做APP的时候,会考虑对页面的打开方向做规定,MUI也给我们提供了很多种页面的打开方式. 具体参考: http://ask.dcloud.net.cn/question/174 MUI做A ...
-
九度oj-1001-Java
题目描述: This time, you are supposed to find A+B where A and B are two matrices, and then count the ...