关于C++的一道题,编写函数reverse(char *s),用递归算法使字符串倒序

来源:百度知道 编辑:UC知道 时间:2024/06/13 15:09:47
#include <iostream>
#include <cstring>
using namespace std;
void reverse(char *s)
{ int N;
char b[]
N=strlen(b);
s=b;
s=s+N;
s++;
N--;
reverse(s);
}
void main()
{
char a[13];
cout <<"输入字符串:";
cin >> a;
cout <<"逆序前为:"<< a <<endl;
reverse(a);
cout <<"逆序后为:"<< a <<endl;
}

好像有错误,高手说一下了

/*错误:char b[]后面少了;还有,少了b数组的长度.
还有,reverse是一库函数,得改一下名.还有,你的程序是不对.你main()函数中用到返回值,但是在reverse()函数中没有返回.我来给你写一段吧,如下:*/
#include <iostream.h>
#include <string.h>
#define max 100

char *sreverse(char *s,int sta,int end)
{
char c,e;
int n=end;
if(end>sta)
{
c=s[sta];
e=s[end];
s[sta]=e;
s[end]=c;
sta++;
end--;
sreverse(s,sta,end);
}
return s;
}
void main()
{
char a[max];
char *p;
cout<<"请输入字符串:";
cin>>a;
cout<<"逆序前为:"<<a<<endl;
p=sreverse(a,0,strlen(a)-1);
cout <<"逆序后为:"<<p<<endl;

}

#include<iostream>
#include<string>
using namespace std;
void daoxu(char *m,int k)
{
int i,j;
for(i=0;i<(k/2);i++)
{
j=*(m+i);
*(m+i)=*(m+k-1-i);
*(m+k-1-i)=j;
}