Third practice 4

Third practice 4

任务描述

编写函数reverse(char *s)的倒序递归程序,使字符串s倒序。

测试输入:输入一个字符串:zxc; 预期输出: 原字符串为:zxc 倒序反转后为:cxz

测试输入:输入一个字符串:machine@##; 预期输出: 原字符串为:machine@## 倒序反转后为:##@enihcam

源代码

#include <iostream>
#include <string>
using namespace std;
void reverse(char *s, char *t)
{
	
}
void reverse(char *s)
{
	char *p1,*p2;
	char temp;
	p1 = p2 = s;
	while(*p2!='\0'){
		p2++;
	}
	p2--;

	while(p1<p2){
		temp = *p1;
		*p1 = *p2;
		*p2 = temp;
		p1++;
		p2--;
	}
}
int main(){
	char a[50];
	cout<<"输入一个字符串:";
	cin>>a;
	cout<<a<<endl;
	cout<<"原字符串为:"<<a<<endl;

	reverse(a);
	cout<<"倒序反转后为:"<<a;
	return 0;
}

posted @ 2020-05-18 15:34  碎了的冰  阅读(155)  评论(0编辑  收藏  举报