C/C++ | 18-3 递归反向输出字符串
2017-07-05 10:32
399 查看
递规反向输出字符串的例子,可谓是反序的经典例程
对于文件操作
/* 递规反向输出字符串 */ #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <assert.h> using namespace std; void inverse(char *p) { if (*p == '\0') return; else { inverse(p + 1);//重点 printf("%c", *p); } } int main() { char str[] = "!1ab sd9"; inverse(str); system("pause"); return 0; }
对于文件操作
/* 递规反向输出字符串 文件为逐行倒叙 */ #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <assert.h> using namespace std; void inverse(FILE *fread,FILE*fwrite) { char buf[1024] = { 0 }; if (!fgets(buf, sizeof(buf), fread)) return; inverse(fread, fwrite); fputs(buf, fwrite); //仍是循环后输出 } int main() { FILE *fr = NULL; FILE *fw = NULL; fopen_s(&fr, "f1.txt", "r"); fopen_s(&fw, "f2.txt", "w"); if (fr == NULL | fw == NULL) return -1; inverse(fr, fw); fclose(fr); fclose(fw); cout << "Done" << endl; system("pause"); return 0; }
相关文章推荐
- C/C++:递归使用:使用递归反向写文件(例子)以及字符串翻转输出
- 递归反向输出字符串
- 递归实现字符串逆序输出(c++和c)
- 递归输入字符串反向输出
- [面试] 递归 反向输出链表 ,定义一个求字符串长度的函数, 反向输出字符串. 不能再函数内部定义变量.
- 【C++】输入并反向输出字符串
- 变成基本功——递归反向输出字符串
- 递归反向输出字符串
- 多种方式实现字符串/无符号数反向输出_栈_递归_头尾指针
- 递归实现反向输出字符串
- 将输入字符串反向保存输出(递归)
- 递归反向输出字符串
- 多种方式实现字符串/无符号数反向输出_栈_递归_头尾指针
- 字符串反向输出(递归)
- C语言---递归反向输出任意长度的字符串
- C语言---递归反向输出任意长度的字符串
- java用递归实现输入字符串反向输出
- 使用递归反向输出一个字符串
- 递归反向输出字符串
- [面试] 递归 反向输出链表 ,定义一个求字符串长度的函数, 反向输出字符串. 不能再函数内部定义变量.