如何写一个程序,使得英文句子逆序输出
类似于:输入you are a champion 输出champion a are you
【注意,空格也要逆序输出】
[解决办法]
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);
}
}