注册 登录
编程论坛 C++教室

大家来找错

十八太保 发布于 2011-07-27 22:51, 357 次点击
#include<iostream>
using namespace std;
char *flip(char *ptr)
{
    char *p1,*p2,temp;
    p1=p2=ptr;
    while (*p2++)
    p2-=2;
    while (p1<p2)
    {
        temp=*p2;
        *p2--=*p1;
        *p1++=temp;
    }
return ptr;
}
void main()
{
    char str[200];
    cout<<"输入字符串:";
        cin.getline(str,200);
            cout<<str<<endl;
        cout<<flip(str)<<endl;   
}
程序实现的是字符串按逆序输出,可本程序不能,大家帮着找找错在哪
6 回复
#2
诸葛修勤2011-07-28 12:05
    while (*p2++);
#3
十八太保2011-07-28 21:13
回复 楼主 十八太保
O(∩_∩)O~  谢谢了,小小的失误造成了整个程序不能正常运行。
#4
xg56992011-07-28 21:29
楼下

[ 本帖最后由 xg5699 于 2011-7-28 21:57 编辑 ]
#5
xg56992011-07-28 21:39
为什么要写的那么复杂呢?
#include <iostream>
#include <string>
using namespace std;

void main()
{
  string a;
  int b;
  cout<<"输入字符串:";
  getline(cin,a);
  b=a.length();
  for(int i=0;i<b;i++)
      cout<<a[b-1-i];
}
这样很简单不就好了吗?

[ 本帖最后由 xg5699 于 2011-7-28 21:59 编辑 ]
#6
十八太保2011-07-28 23:11
回复 4楼 xg5699
练习嘛,用不同的方法实现同一个程序
#7
xg56992011-07-29 14:01
回复 6楼 十八太保
感觉你的程序写的比较深奥,我一直看不懂 while(*p2++)是什么意思?

https://bbs.bccn.net/thread-346240-1-1.html

这个帖子的楼主也是用返回char指针来实现输入的字符串倒过来输出的,一看就懂.
1