字符串逆置问题

liloveli0928 2010-07-10 07:01:55
#include<iostream.h>
#include<stdio.h>
#include<string.h>
void main()
{
void revstr(char *s);
char a[81];
gets(a);
char *s;//定义一个指向数组开头的指针
s=a;
revstr(s);
}
void revstr(char *s)
{
char *p,c,*q;
q=s;
p=s+strlen(s)-1;//指向数组末的指针
while(s==NULL)
{
return;
}
while(s<p);//逆置的实现
{
c=*s;
*(s++)=*p;
*(p--)=c;
}
while(*q!='\0')//输出逆置后的字符串
{
cout<<*(q++);
}
}
想用指针实现字符串逆置。但是写了以上的东西自己感觉没错但是就是无法做出结果,求助。
...全文
280 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liloveli0928 2010-07-10
  • 打赏
  • 举报
回复
果然是逗号的问题。我真粗心。~
cattycat 2010-07-10
  • 打赏
  • 举报
回复
while(s<p);//逆置的实现
多了个分号。
woniu11z 2010-07-10
  • 打赏
  • 举报
回复
把分号去掉!
woniu11z 2010-07-10
  • 打赏
  • 举报
回复
while(s<p);//逆置的实现
把逗号去掉!
woniu11z 2010-07-10
  • 打赏
  • 举报
回复
while(s<p);//逆置的实现把逗号去掉!
elegant87 2010-07-10
  • 打赏
  • 举报
回复

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

void revstr(char *s);

int main()
{
char a[81];
gets(a);
revstr(a);
cout<<endl;
return 0;
}

void revstr(char *s)
{
int len=strlen(s)/2;;
char *p,c,*q;
q=s;
p=s+strlen(s)-1;//指向数组末的指针
if(!s)
return;
cout<<len<<endl;
for(int i=0;i<len;++i)
{
c=*s;
*s++=*p;
*p--=c;
}

s=q;
while(*s!='\0')//输出逆置后的字符串
{
cout<<*s++;
}
}
tan625747 2010-07-10
  • 打赏
  • 举报
回复
帮你改了一下
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

void main()
{
void revstr(char *s);
char a[81];
gets(a);
char *s;//定义一个指向数组开头的指针
s=a;
revstr(s);
}

void revstr(char *s)
{
char *p,c,*q;
q=s;
p=s+strlen(s)-1;//指向数组末的指针
while(s==NULL)
{
return;
}
do//逆置的实现
{
c=*s;
*(s++)=*p;
*(p--)=c;
}while(*s<*p);

while(*q!='\0')//输出逆置后的字符串
{
cout<<*(q++);
}
}




do//逆置的实现
{
c=*s;
*(s++)=*p;
*(p--)=c;
}while(*s<*p);

chaoliu1024 2010-07-10
  • 打赏
  • 举报
回复
指针p,q没有分配空间

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧