C 练习实例27
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
程序分析:无。
实例
// Created by study.p2hp.com on 15/11/9.
// Copyright © 2015年 高手教程. All rights reserved.
//
#include <stdio.h>
int main()
{
int i=5;
void palin(int n);
printf("请输入5个字符\40:\40");
palin(i);
printf("\n");
}
void palin(n)
int n;
{
char next;
if(n<=1) {
next=getchar();
printf("相反顺序输出结果\40:\40");
putchar(next);
} else {
next=getchar();
palin(n-1);
putchar(next);
}
}
以上实例输出结果为:
请输入5个字符 : abcde 相反顺序输出结果 : edcba
C 语言经典100例



永恒的灯火
163***3217@qq.com
参考方法:
#include<stdio.h> void fun(char* s,int length) { if (length >= 1) { printf("%c\n", s[length - 1]); fun(s, length - 1); } } int main() { char* s = "hello"; int length = 5; fun(s, length); return 0; }永恒的灯火
163***3217@qq.com
斜阳
553***353@qq.com
参考方法:
#include <stdio.h> void f() { char ch; if((ch=getchar())!='\n') { f(); } if(ch!='\n') { printf("%c",ch); } } void main() { printf("请输入字符: "); f(); }斜阳
553***353@qq.com