首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么写一个程序,使得英文句子逆序输出

2013-01-08 
如何写一个程序,使得英文句子逆序输出类似于:输入you are a champion输出champion a are you【注意,空格也

如何写一个程序,使得英文句子逆序输出
类似于:输入you are a champion  输出champion a are you
【注意,空格也要逆序输出】
[解决办法]
std::reverse满足你的要求~~
[解决办法]

引用:
应该怎样应用??我只会数字的倒序输出

可以使用strstr,循环调用strstr,每次的结果放进vector,然后再用std::reverse,再按顺序输出。

[解决办法]

#include <iostream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>

int main()
{
   std::istream_iterator<std::string> st(std::cin), end;
   //从控制台接受输入,直到输入流结束(Ctrl+Z)
   std::vector<std::string> words(st,end);
    //按单词逆序输出
    std::reverse_copy(words.begin(),words.end(),
       std::ostream_iterator<std::string>(std::cout," "));
    std::cout << std::endl;
    return 0;
}
//rock and roll
//^Z
//roll and rock


[解决办法]

void RS(char *bp, char *ep)
{
while(bp < ep)
{
char tem = *bp;


*bp = *ep;
*ep = tem;

bp++;
ep--;
}
}
void Reverse(char *s)
{
int len = strlen(s);
char *es = s + len -1;

RS(s,es);

char *p1 = s;
char *p2 = s;

while( *p2 != '\0')
{
while(*p2 != '\0' && *p2 != ' ')
p2++;

RS(p1,p2-1);

if( *p2 == ' ' && *p2 != '\0')
{
p2++;
p1 = p2;
}
}
}


char test[] = "you are a champion ";
Reverse(test);


[解决办法]
先将整个句子翻转,再逐个翻转单词;或者先逐个翻转单词,再翻转真个句子

#include <stdio.h>
#include <string.h>

char s[50001];

void reverse(int begin,int end)
{
while(begin<end)
{
char temp=s[begin];
s[begin]=s[end];
s[end]=temp;
++begin;
--end;
}
}

int main()
{
while(gets(s))
{
int len=strlen(s),begin=0;
reverse(0,len-1);
for(int i=0;i<len;i++)
{
if(s[i]!=' ')
{
if(i>0&&s[i-1]==' ')
begin=i;
else if(i<len-1&&s[i+1]==' ' 
[解决办法]
 i==len-1)
reverse(begin,i);
}
}
printf("%s\n",s);
}
}

热点排行