C++编程题 reverse

最好用C++来写啊。。。 能用库函数的尽量用。。。 求大神帮忙= =

第1个回答  2014-03-21
std::string s;
using std::cin;
while(cin>>s){
    reverse(s.begin(),s.end());
    std::cout<<s;
    while(char c=cin.get())
        if(not std::isspace(c)){
            cin.putback(c);
            break;
        }else std::cout<<c;    
}

结果处理空格的代码比反转字符串的代码还长……用std::stringstream大概可以减少代码,懒得试了

追问

我还是个新手。。。 能复制个完整的代码吗

追答#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
 
int main(){
    std::string s;
    using std::cin;
    while(cin>>s){
        reverse(s.begin(),s.end());
        std::cout<<s;
        while(char c=cin.get())
            if(not std::isspace(c)){
                cin.putback(c);
                break;
            }else std::cout<<c;    
    }
}

第2个回答  2014-03-21
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main() {
int n;
    char s[1050],w[1050];
scanf("%d", &n);
printf("%d\n", n);
gets(s);

for (int i=0; i<n; i++) {
gets(s);
int l = strlen(s);
s[l] = '\n';

int l2 = 0;
for (int j=0; j<=l; j++)
if (s[j]!=' ' && s[j]!='\n')
w[l2++] = s[j];
else {
for (int k=l2-1; k>=0; k--) printf("%c", w[k]);
printf("%c", s[j]);
l2 = 0;
}
}
return 0;
}

翻转这个功能……还是放弃库吧……

本回答被提问者采纳
第3个回答  2014-03-21
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
size_t n,i;
cin>>n;
string *p=new string[n],s;
char c;
for(i=0;i<n;++i)
{
cin>>*(p+i);
reverse((p+i)->begin(),(p+i)->end());
for(;(c=cin.get())==' ';)
{
cin>>s;
reverse(s.begin(),s.end());
*(p+i)+=" ";
*(p+i)+=s;
}
}
cout<<n<<endl;
for(i=0;i<n;++i)
{
cout<<*(p+i)<<endl;
}
return 0;
}
只说一句,好有爱的题目。。。
相似回答