《C和指针》章节后编程练习解答参考——6.3
《C和指针》——6.3
题目:
编写一个函数,把参数字符串中的字符反向排列。
函数原型:
void reverse_string(char *string);
要求:
使用指针而不是数组下标
不要使用任何C函数库中用于操纵字符串的函数
不要声明一个局部数组来临时存储参数字符串
解答代码:
#include <stdio.h> void reverse_string(char *string)
{
int i, n=; while (*(string+n) != '\0') //计算字符串中字符的个数
n++;
n--; //字符个数n作为索引号时要减1,即从0到n-1
if (n > ) //字符个数小于等于1时没有必要反转
{
for (i=; i<=(n/); i++)
{
if (i != (n-i))
{
char p; //字符内容交换
p = *(string+i);
*(string+i) = *(string+n-i);
*(string+n-i) = p;
}
}
}
} int main()
{
char source[] = "ABCDEFGH"; printf("Before reverse:\n%s\n", source);
reverse_string(source);
printf("After reverse:\n%s\n", source); getchar();
return ;
}
注:
1、先计算字符串中非'\0'字符的个数
2、首位字符轮流交换
《C和指针》章节后编程练习解答参考——6.3的更多相关文章
- 《C和指针》章节后编程练习解答参考——6.2
<C和指针>——6.2 题目: 编写一个函数,删除源字符串中含有的子字符串部分. 函数原型: int del_substr(char *str, char const *substr); ...
- 《C和指针》章节后编程练习解答参考——第5章
5.1 题目: 略 解答代码: #include <stdio.h> int main(void) { char ch; while (((ch = getchar()) != EOF) ...
- 《C和指针》章节后编程练习解答参考——6.6
<C和指针>——6.6 题目: 在指定的下限.上限之间使用数组方法查找质数,并将质数提取出来. 要求: 略 解答代码: #include <stdio.h> #define U ...
- 《C和指针》章节后编程练习解答参考——6.4
<C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...
- 《C和指针》章节后编程练习解答参考——6.1
<C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...
- 《C和指针》章节后编程练习解答参考——第10章
10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...
- 《C和指针》章节后编程练习解答参考——第9章
9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...
- 《C和指针》章节后编程练习解答参考——第8章
8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...
- DSAPI多功能组件编程应用-参考-Win32API常数
DSAPI多功能组件编程应用-参考-Win32API常数 在编程过程中,常常需要使用Win32API来实现一些特定功能,而Win32API又往往需要使用一些API常数,百度搜索常数值,查手册,也就成了 ...
随机推荐
- c#问答篇:对象与引用变量-----初学者的困惑
转自:http://www.cnblogs.com/huangyu/archive/2004/08/02/29622.html 从宏观的角度来看,对象是类的实例.比如: //定义一个名为Someone ...
- 同步窗体移动 FormMove
方法2 unit Unit1;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Sy ...
- Ubuntu 12.04 升级到14.04之后,pidgin-sipe 出现的问题: Trouble with the pidgin and self-signed SSL certificate
Once again, I run into trouble when upgrading my LinuxMint. In last few days, my Linux mint notifies ...
- [Javascript] Immute Object
Three ways to make object immutable: 1. Use JSON.parse(JSON.stringify(obj)): this approach is little ...
- Linux系统及应用问题分析排查工具
linux 阿里技术协会 摘要: Linux服务器上经常遇到一些系统和应用上的问题,如何分析排查,需要利器,下面总结列表了一些常用工具.trace tool:最后也列举了最近hadoop社区在开发发展 ...
- 查看MySQL数据库表的命令介绍
如果需要查看MySQL数据库中都有哪些MySQL数据库表,应该如何实现呢?下面就为您介绍查看MySQL数据库表的命令,供您参考. 进入MySQL Command line client下查看当前使用的 ...
- Docker中开启sshd服务
ssh服务安装 安装ssh服务 #yum install openssh-server -y 安装passwd(修改密码需要) #yum install passwd -y 修改sshd_config ...
- Openfire3.8.2在eclipse中Debug方式启动最简单的方式
一.前言 最近打算研究一下Openfire,于是打算最好能够以Debug方式启动Openfire的Server,到网上一搜,还果真早到官网的一篇文章来: http://community.ignite ...
- magento addFieldToFilter()方法常用的过滤条件
记录一下Magento模型集合Model Collection中addFieldToFilter()方法常用的过滤条件.以下参数也同样适用于产品实体的addAttributeToFilter()方法. ...
- Java联网技术之一HTTP
学到Java的联网技术,这里首先来看看关于URl, 要从网上获得内容, 需要实现下面的4步, 1.创建一个表示资源的网络地址的URL对象, 2.创建一个HttpURLConnection 连接对象 3 ...