ACM水题 ---反转文本

jizhuzhong 2013-03-10 10:58:19
反转文本:就是假如输入12345,应当输出54321
一下是我的代码,我觉得没问题,可是就是不能实现反转,求各位大神帮帮忙!!!谢谢!
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s;
char ss[80];
int n;
cin>>n;//输入字符串的个数

for(int i=0;i<n;i++)
{
cin.getline(ss,80);

s=ss;
reverse(s.begin(),s.end());//反转
cout<<s<<endl;
}
return 0;
}
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
秃头披风侠 2013-03-11
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

const int MAX=1024;

void swap(char *c1, char *c2)
{
        char tmp = *c1;
        *c1 = *c2;
        *c2 = tmp;
}

int main()
{
        char str[MAX] = {0};
        cin >> str;

        int len = 0;
        for (int i=0; i<MAX; i++)
        {   
                if (str[i] == '\0')
                {   
                        break;
                }   
                len++;
        }   
        cout << len << endl;
        cout << str << endl;

        for (int i=0; i<len/2; i++)
        {   
                swap(&str[i], &str[len - i - 1]);
        }   

        cout << str << endl;

        return 0;
}

mujiok2003 2013-03-11
  • 打赏
  • 举报
回复
输入部分没有弄好啊。
#include<iostream>
#include<string>
#include<algorithm>
using  namespace   std;
int  main()
{
  string s;
  int n;
  cin >> n;//输入字符串的个数 
    
  while(n > 0 && getline(std::cin, s))
  {
    reverse(s.begin(),s.end());//反转
    std::cout << s << std::endl;
  }
  return 0;
} 
scream7 2013-03-11
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
#include<algorithm>
using  namespace   std;
int  main()
{
  string  s;
  char  ss[80];
  int n;
  cin>>n;//输入字符串的个数

  for(int i=0;i<n;i++)
  {
    cin.clear();
    cin.ignore(80, '\n');
    cin.getline(ss,80);

    s=ss;
    reverse(s.begin(),s.end());//反转
    cout<<s<<endl;
  }
  return 0;
}
scream7 2013-03-11
  • 打赏
  • 举报
回复

for(int i=0;i<n;i++)
{
 cin.clear();//试试
 cin.getline(ss,80);

 s=ss;
 reverse(s.begin(),s.end());//反转
 cout<<s<<endl;
}
OJ题我还是比较推荐用scanf/printf的
赵4老师 2013-03-11
  • 打赏
  • 举报
回复
strrev
CoolEgos 2013-03-10
  • 打赏
  • 举报
回复
多少题?是不是字符串越界?

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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