《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常数,百度搜索常数值,查手册,也就成了 ...
随机推荐
- How Network Load Balancing Technology Works--reference
http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balan ...
- iOS 网络编程:NSURLConnection
1 简介 1.1 概念 NSURLConnection类似NSURLSession,都是进行网络数据传输的.其中NSURLSession是NSURLConnection的替代版本,目前IOS9.0几乎 ...
- iOS开发UI篇-tableView在编辑状态下的批量操作(多选)
先看下效果图 直接上代码 #import "MyController.h" @interface MyController () { UIButton *button; } @pr ...
- 通用的 makefile 小工具分享 - Easymake 使用说明
Easymake 使用说明 介绍 Easymake 是一个在linux系统中 C/C++ 开发的通用 makefile.在一个简单的 C/C++ 程序中使用 easymake,你甚至可以不写一行 ma ...
- ArrayList的深度copy和浅度拷贝
ArrayList的浅度拷贝方式: 通过Collections.copy方法实现浅度拷贝 ArrayList<GuideGroup> questionGuideGroupList = ne ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- 点击按钮弹出一个div层
JQuery弹出层,点击按钮后弹出遮罩层,还有关闭按钮 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...
- react 编写组件 五
看以下示例了解如何定义一个组件 // 定义一个组件LikeButton var LikeButton = React.createClass({ // 给state定义初始值 getInitialSt ...
- UML 行为图之用例图 总结
学习UML图形 推荐阅读<UML参考手册>第2版. http://www.umlchina.com/ 推荐微软的开发软件设计模型 http://msdn.microsoft.com/zh ...
- Android开发5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...